首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

简单有关问题 GridView中的RowDeleting删除代码出现异常

2012-01-01 
简单问题 GridView中的RowDeleting删除代码出现错误。我需要用代码来实现删除功能。后台代码;protected void

简单问题 GridView中的RowDeleting删除代码出现错误。
我需要用代码来实现删除功能。
后台代码;
protected void GridView1_RowDeleting(object sender GridViewDeleteEventArgs e)
  {
  int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
  user.dele(id);//调用删除方法
  bind();//绑定代码
  }
html代码如下:<asp:GridView ID="GridView1" runat="server" Height="86px" Width="514px" AutoGenerateColumns="false"OnRowDeleting="GridView1_RowDeleting">
  <Columns>
  <asp:BoundField DataField="ID" HeaderText="ID号码" />
  <asp:BoundFieldDataField="Name"HeaderText="姓名" />
  <asp:BoundField DataField="tel" HeaderText="tel" />
  .....
  <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
  </Columns>
  </asp:GridView>
运行出错:
索引超出范围。必须为非负值并小于集合大小。
参数名: index 
请高手指导下,以前都用无代码实现的。现在用代码来实现就出错了。

[解决办法]
你必须为gridview设置一个datakeynames,象下面的代码一样

HTML code
<asp:GridView DataKeyNames="MemberID" AutoGenerateColumns="false" ID="GridView1" runat="server">
[解决办法]
C# code
GridView1.DataKeys[e.RowIndex].Value); 

热点排行