在DataGrid的使用中,出现:索引超出范围。

当用到:DataGrid1.DataKeys.Item(e.Item.ItemIndex)的时候,往往出现:

索引超出范围。必须为非负值并小于集合大小。参数名: index


曾经一段时间百思不得其解,后来才发现……在DataGrid中要设定:DataKeyField为你要取到的字段。

比如在删除的时候用如下代码(为了取得删除按钮所在行的id值):

  Private Sub DataGrid1_DeleteCommand(ByVal source As ObjectByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
        del_k(DataGrid1.DataKeys.Item(e.Item.ItemIndex))
    
End Sub

如果不设定DataKeyField,则返回上述错误,所以,在设计器中,把DataGrid的DataKeyField设定为“id”,问题就解决了。

posted on 2006-08-22 12:50  LiAnGcAt  阅读(655)  评论(0编辑  收藏  举报

导航