如果你仅仅只要添加checkbox的话,我有个简单的方法。 首先在数据源上添加一列,类型为bool型 示例代码: DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("check", typeof(bool)); for (int i = 0; i < 10; i++) { dt.Rows.Add(new object[] { i, true }); } GridView1.DataSource = dt; GridView1.DataBind(); 这样,绑定后的GridView就有一列CheckBox了,但是他是不可用的,还须要手动改成可选的 在RowCreated事件里,将CheckBox改成可用的 示例: protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { foreach(TableCell cell in e.Row.Cells) { if (cell.Controls.Count > 0) { CheckBox c = (CheckBox)cell.Controls[0]; if (c != null) c.Enabled = true; } } }
更简单的: 绑定前添加一空列 BoundField b = new BoundField(); b.HeaderText = "CheckBox"; GridView1.Columns.Add(b); 然后 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.Header) { CheckBox c = new CheckBox(); e.Row.Cells[0].Controls.Add(c); } }
CheckBoxField b = new CheckBoxField(); b.HeaderText = "CheckBox"; GridView1.Columns.Add(b); 我这么弄可以吗
CheckBoxField b = new CheckBoxField(); b.HeaderText = "CheckBox"; GridView1.Columns.Add(b); 我这么弄可以吗=============当然不可以绑定前添加一空列 BoundField b = new BoundField(); b.HeaderText = "CheckBox"; GridView1.Columns.Add(b); GridView1.DataSouse=...; GridView1.DataBind();在RowCreated事件里 if (e.Row.RowType != DataControlRowType.Header) { CheckBox c = new CheckBox(); e.Row.Cells[0].Controls.Add(c); }
http://www.cnblogs.com/lovecherry/archive/2005/03/25/125526.html
http://topic.csdn.net/t/20050602/08/4052868.html
首先在数据源上添加一列,类型为bool型
示例代码:
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("check", typeof(bool));
for (int i = 0; i < 10; i++) {
dt.Rows.Add(new object[] { i, true });
}
GridView1.DataSource = dt;
GridView1.DataBind();
这样,绑定后的GridView就有一列CheckBox了,但是他是不可用的,还须要手动改成可选的
在RowCreated事件里,将CheckBox改成可用的
示例:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
foreach(TableCell cell in e.Row.Cells)
{
if (cell.Controls.Count > 0) {
CheckBox c = (CheckBox)cell.Controls[0];
if (c != null)
c.Enabled = true;
}
}
}
绑定前添加一空列
BoundField b = new BoundField();
b.HeaderText = "CheckBox";
GridView1.Columns.Add(b);
然后
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType != DataControlRowType.Header) {
CheckBox c = new CheckBox();
e.Row.Cells[0].Controls.Add(c);
}
}
b.HeaderText = "CheckBox";
GridView1.Columns.Add(b);
我这么弄可以吗
b.HeaderText = "CheckBox";
GridView1.Columns.Add(b);
我这么弄可以吗=============当然不可以绑定前添加一空列
BoundField b = new BoundField();
b.HeaderText = "CheckBox";
GridView1.Columns.Add(b);
GridView1.DataSouse=...;
GridView1.DataBind();在RowCreated事件里
if (e.Row.RowType != DataControlRowType.Header) {
CheckBox c = new CheckBox();
e.Row.Cells[0].Controls.Add(c);
}
请问这是什么原因呢
请问这是什么原因呢该写的代码都写了阿!
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
CheckBox c = new CheckBox();
e.Row.Cells[0].Controls.Add(c);
}
} BoundField b = new BoundField();
b.HeaderText = "CheckBox";
GridView1.Columns.Add(b);
GridView1.DataSource = dtst.Tables[0].DefaultView;
GridView1.DataBind();
请高人帮看看,现在能加上一列了 但是不是checkbox的
中的cells[0]0代表第1列