怎么读取gridview里的一行ID啊?
我用gridview现实一个表,现在要更新表中的数据,自己定义了update的函数。。
<asp:BoundField HeaderText= "No " DataField= "DateId " ReadOnly= "True " />
<asp:TemplateField HeaderText= "Date ">
<ItemTemplate>
<%# Eval( "WDate ")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID= "WDate " Text= ' <%# Eval( "WDate ")%> ' runat= "server " Width= "140px " />
</EditItemTemplate>
<ItemStyle Width= "150px " />
</asp:TemplateField>
然后我更新的地方
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Values[0];
string date = ((TextBox)GridView1.Rows[e.RowIndex].FindControl( "WDate ")).Text;
string wdescription = ((TextBox)GridView1.Rows[e.RowIndex].FindControl( "WDescription ")).Text;
string status = ((TextBox)GridView1.Rows[e.RowIndex].FindControl( "Status ")).Text;
UpdateRecords();//这个是更新的函数。
GridView1.EditIndex = -1;
BindGrid();
}
运行的时候说我
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
问题就是第一行:int id = (int)GridView1.DataKeys[e.RowIndex].Values[0];
怎么办啊?高手请指教啊。。
------解决方案--------------------CheckBox cb = (CheckBox)GridView1.Rows[0].FindControl( "CheckBox1 ");
------解决方案--------------------如果有多行
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl( "CheckBox1 ");
if (cb.Checked)
{
}
}
------解决方案--------------------int id = (int)GridView1.DataKeys[e.RowIndex].Value; 这样试试
------解决方案--------------------int id = (int)GridView1.DataKeys[i][0].ToString()
------解决方案--------------------int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.toString());
------解决方案--------------------帮顶 !
------解决方案--------------------同意ls
差不多
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);