如何添加复选框字段在GridView的? [英] How to Add the CheckBox Field in GridView?
问题描述
如何添加复选框字段在GridView控件编程,有什么错我的代码?
试
{
串DATA_SOURCE = @数据源= A-63A9D4D7E7834\SECOND;
串Initial_Catalog = @初始目录=复制;;
字符串用户= @用户ID = SA;;
字符串密码= @密码=二;
串full_con = DATA_SOURCE + Initial_Catalog +用户+密码;
SqlConnection的连接=新的SqlConnection(full_con);
connection.Open();
的SqlCommand numberofrecords =新的SqlCommand(SELECT COUNT(*)FROM dbo.Table_1连接);
的数据集DS2 =新的DataSet();
SqlDataAdapter的testadaptor =新的SqlDataAdapter();
testadaptor.SelectCommand =新的SqlCommand(SELECT COUNT(*)FROM dbo.Table_1连接);
testadaptor.Fill(DS2);
grid1.DataSource = DS2;
CheckBoxField字段C =新CheckBoxField字段();
grid1.Columns.Add(三);
grid1.DataBind();
numberofrecords.Dispose();
的Connection.close();
connection.Dispose();
}
赶上(例外)
{
的Response.Write(请检查);
的Response.Write(a.Message.ToString());
的Response.Write(a.Source.ToString());
} //抓
在CheckBoxField字段会可能要为数据字段属性的值。这应该与在查询中的列名或别名。 (我不认为一个复选框将与多项成果的工作,虽然)
编辑:不知道你想干什么。模板领域和常规复选框应该让你更接近你想要什么。事情是这样的。
例如
const int的ColumnSelect = 0。
保护无效的Page_Load(对象发件人,EventArgs五)
{
//此处获取真实的数据。
DataTable的DT =新的DataTable();
dt.Columns.Add(伯爵);
dt.Rows.Add(dt.NewRow());
dt.Rows [0] [0] =10;
GridView1.Columns.Add(新的TemplateField());
绑定列B =新的BoundField();
GridView1.Columns.Add(二);
b.DataField =伯爵;
GridView1.DataSource = DT;
GridView1.DataBind();
}
保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
如果(e.Row.RowType!= DataControlRowType.Header)
{
e.Row.Cells [ColumnSelect] .Controls.Add(新的复选框());
}
}
编辑#2:为获取值,你当然可以做到这一点。您是否正在寻找一个Javascript或服务器端解决方案?下面是服务器端的一个简单的例子,如果你有一个按钮,点击:
保护无效的button1_Click(对象发件人,EventArgs五)
{
的foreach(GridViewRow行GridView1.Rows)
{
//也可以使用(复选框)row.Cells [ColumnSelect] .FindControl如果你给的复选框的ID生成时他们。
复选框CB =(复选框)row.Cells [ColumnSelect] .Controls [0];
如果(cb.Checked)
{
//这里做一些事情。
}
}
}
How to add the checkbox field in gridview programatically, what's wrong with my code?
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. 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?
e.g.
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. Are you looking for a Javascript or server-side solution? 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.
}
}
}
这篇关于如何添加复选框字段在GridView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!