在DataGrid里的EditCommand写了一语句获取主键值,结果运行老是提示出错错误信息是:索引超出范围。必须为非负值并小于集合大小。参数名: index这个是什么情况?高手给指点指点 private void dg_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ssslink =(string)dg.DataKeys[(int)e.Item.ItemIndex];
}

解决方案 »

  1.   

    what is your e.Item.ItemType and e.Item.ItemIndex value? did you set DataKeyField property for your DataGrid?
      

  2.   

    先判断一下e.Item.ItemIndex是否大于-1.
      

  3.   

    绑定DATAGRID时定义一个
    this.DataGrid1.DataKeyField="你下面要用到的键值";
    然后下面调用时用这个
    string ssslink=this.DataGrid1.DataKeys[e.Item.ItemIndex].toString();这样就行。
      

  4.   

    电击DataGrid的属性在DataGrid属性中的DataKeyField
      

  5.   

    电击DataGrid的属性在DataGrid属性中的DataKeyField一般设置你的表中的主键
      

  6.   

    e.Item.ItemIndex的值都正常,我用Label输出了,就是 用Datakeys获取的时候不行
      

  7.   

    .Item.ItemIndex的值都正常,我用Label输出了,就是 用Datakeys获取的时候不行--------------------
    你绑定的时候设置了主键吗?
    this.DataGrid.DataSource = dt;
    this.DataGrid.DataKeyField = "ID";
    this.DataGrid.DataBind();
      

  8.   

    恩,确实是DataKeyField忘记设了,谢谢各位指点