有关gridview控件的问题??
来源:百度知道 编辑:UC知道 时间:2024/05/23 23:01:01
gridview更新代码里面有段这样的代码:
string sqlstr = "update staff set staff_id='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()+ "',staff_name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()+ "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
我编译的时候提示这样的错误:
指定的参数已超出有效值的范围。
参数名: index
在网上搜了好像是要用findcontrol,我想问下如果用findcontrol更改上面的代码的话 怎样改呀?为什么用.Cells[2].Controls[0]))就会出错??
string sqlstr = "update staff set staff_id='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()+ "',staff_name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()+ "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
我编译的时候提示这样的错误:
指定的参数已超出有效值的范围。
参数名: index
在网上搜了好像是要用findcontrol,我想问下如果用findcontrol更改上面的代码的话 怎样改呀?为什么用.Cells[2].Controls[0]))就会出错??
((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("txtstuAge")).Text;
txtstuAge是你GridView1里模板列里编辑项textbox的ID
.Cells[2].findcontrol("控件ID")
和.Cells[2].Controls[0]))是一样的。。。唯一不同的是后者你需要知道这个控件在这一row.cell[i]里是第几个控件
C#的索引是从0开始的,要小心