在Datagrid控件上添加了linkbutton,实现删除功能,老是出现这个问题.头都大了,特来求救``
哦米佗佛,善哉善哉.Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: indexSource Error: 
Line 168: protected void DataGrid1_DeleteCommand(Object sender,DataGridCommandEventArgs e)
Line 169: {
Line 170: int ID =(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
Line 171:
Line 172: Conn con=new Conn();
 Source File: c:\inetpub\wwwroot\richscrm\richs\admin\adminspeo.aspx.cs    Line: 170 Stack Trace: 
[ArgumentOutOfRangeException: Index was out of range.  Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ArrayList.get_Item(Int32 index) +91
   System.Web.UI.WebControls.DataKeyCollection.get_Item(Int32 index) +10
   admin.AdminSPeo.DataGrid1_DeleteCommand(Object sender, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\richscrm\richs\admin\adminspeo.aspx.cs:170
   System.Web.UI.WebControls.DataGrid.OnDeleteCommand(DataGridCommandEventArgs e) +110
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +589
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
   System.Web.UI.Page.ProcessRequestMain() +1277 
谢答,过多垃圾信息还望眼谅

解决方案 »

  1.   

    protected void DataGrid1_DeleteCommand(Object sender,DataGridCommandEventArgs e)
    {
    int ID =(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];//这句始终出问题,头都大了 Conn con=new Conn(); con.Execute("DELETE FROM 员工档案 WHERE ID="+ID);

    Bindata();  }
      

  2.   

    int ID = Convert.ToInt32(DataGrid1.Items[e.Item.ItemIndex].Cells[0].Text);//假定你在DataGrid的第一列放了ID
      

  3.   

    你的DataGrid的DataKey这个属性设置了?
      

  4.   

    问题搞定了,DataKeyFiled填充DataKeys的字段没设,谢谢两位