[英]How to Add the CheckBox Field in GridView?
How to add the checkbox field in gridview programatically, what's wrong with my code? 如何以编程方式在gridview中添加复选框字段,我的代码有什么问题?
try
{
string Data_source=@"Data Source=A-63A9D4D7E7834\SECOND;";
string Initial_Catalog=@"Initial Catalog=replicate;";
string User=@"User ID=sa;";
string Password=@"Password=two";
string full_con=Data_source+Initial_Catalog+User+Password;
SqlConnection connection = new SqlConnection(full_con);
connection.Open();
SqlCommand numberofrecords = new SqlCommand("SELECT COUNT(*) FROM dbo.Table_1", connection);
DataSet ds2 = new DataSet();
SqlDataAdapter testadaptor = new SqlDataAdapter();
testadaptor.SelectCommand = new SqlCommand("SELECT COUNT(*) FROM dbo.Table_1", connection);
testadaptor.Fill(ds2);
grid1.DataSource = ds2;
CheckBoxField c = new CheckBoxField();
grid1.Columns.Add(c);
grid1.DataBind();
numberofrecords.Dispose();
connection.Close();
connection.Dispose();
}
catch (Exception a)
{
Response.Write("Please check ");
Response.Write(a.Message.ToString());
Response.Write(a.Source.ToString());
}//catch
The CheckBoxField will probably want a value for the DataField property. CheckBoxField可能需要DataField属性的值。 This should match the column names or aliases in your query. 这应该与查询中的列名或别名匹配。 (I don't think a checkbox will work with number results, though.) (不过,我认为复选框不会与数字结果一起使用。)
Edited: didn't realize what you were trying to do. 编辑:没意识到你想做什么。 A template field and a regular checkbox should get you closer to what you want. 模板字段和常规复选框应使您更接近所需的内容。 Something like this? 像这样吗
eg 例如
const int ColumnSelect = 0;
protected void Page_Load(object sender, EventArgs e)
{
//Get real data here.
DataTable dt = new DataTable();
dt.Columns.Add("count");
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = "5";
GridView1.Columns.Add(new TemplateField());
BoundField b = new BoundField();
GridView1.Columns.Add(b);
b.DataField = "count";
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Cells[ColumnSelect].Controls.Add(new CheckBox());
}
}
Edit #2: as for getting the value, you can certainly do this. 编辑#2:关于获取值,您当然可以这样做。 Are you looking for a Javascript or server-side solution? 您在寻找Javascript还是服务器端解决方案? Here's a simple example for server-side if you had a button click: 如果您单击按钮,这是服务器端的一个简单示例:
protected void Button1_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows)
{
//Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
CheckBox cb = (CheckBox)row.Cells[ColumnSelect].Controls[0];
if (cb.Checked)
{
//Do something here.
}
}
}
I had to specify which checkbox object, like this 我必须指定哪个复选框对象,像这样
System.Web.UI.WebControls.CheckBox
Also I had to add this to the gridview aspx page 我也必须将其添加到gridview aspx页面
OnRowDataBound="GridView1_RowDataBound"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.