日期:2014-05-18 浏览次数:21018 次
DataSet ds = new DataSet();
ds.Tables.Add("namelist");
ds.Tables(0).Columns.Add("ID", typeof(Int32));
ds.Tables(0).Columns.Add("Name", typeof(string));
ds.Tables(0).Columns.Add("c", typeof(bool));
ds.Tables(0).Rows.Add(new object[] { 1, "Li", 0 });
ds.Tables(0).Rows.Add(new object[] { 2, "Lu", 1 });
GridView1.DataSource = ds;
GridView1.DataBind();
foreach (GridViewRow gr in GridView1.Rows) {
CheckBox ckb = (CheckBox)gr.Cells(2).Controls(0);
if (ckb.Checked) {
string strName = gr.Cells(1).Text.ToString;
Response.Write(strName);
}
}
------解决方案--------------------
非常正确呀。
------解决方案--------------------
如果已经是模板列,用Cells[index].Text是得不到里面的内容的,必须使用FindControl方法,其实理由很简单,TemplateField里面可以有多个控件,比如文本框,标签等等,你取里面的Cells[index].Text,取的算是什么呢?
CheckBox chb;
string stuID = string.Empty;
string stuName = string.Empty;
foreach (GridViewRow gr in GridView1.Rows)
{
chb = gr.FindControl("chb_check") as CheckBox;
if (chb != null && chb.Checked)
{
stuID = (gr.FindControl("id文本框的id") as TextBox).Text;
stuName = (gr.FindControl("name文本框的id") as TextBox).Text;
}
}
------解决方案--------------------
foreach(GridViewRow gr in GridView1.Rows)
{
CheckBox chb = (CheckBox)gr.FindControl("chb_check");
if (chb.Checked)
{
string stuID = chb.Text.ToString();
string stuName = chb.Text.ToString();
}
}
这样试一试 。。。