日期:2014-05-18  浏览次数:20474 次

gridview删除问题,在线急等
在用户表里查询出所有信息,然后点击删除想把记录删除,为什么点击删除就会报错,提示 "索引超出范围。必须为非负值并小于集合大小
参数名:   index   "这是查询记录的代码
  Public   Sub   linker()
                Dim   conn   As   New   SqlConnection
                Dim   sql   As   String
                conn.ConnectionString   =   "data   source=192.168.31.199;initial   catalog=kaoqin   manager;user   id=james5;password=james5; "
                sql   =   "select   *   from   denglu "
                Dim   cmd   As   New   SqlCommand(sql,   conn)
                conn.Open()
                Dim   dr   As   New   SqlDataAdapter(cmd)
                Dim   dl   As   New   DataSet
                dr.Fill(dl)
                GridView1.DataSource   =   dl.Tables(0).DefaultView
                GridView1.DataBind()
                conn.Close()
        End   Sub
这是删除代码:
  Protected   Sub   GridView1_RowDeleting(ByVal   sender   As   Object,   ByVal   e   As   System.Web.UI.WebControls.GridViewDeleteEventArgs)   Handles   GridView1.RowDeleting
                Dim   sql   As   String
                Dim   id   As   Integer   =   GridView1.DataKeys(e.RowIndex).Value
                sql   =   "delete   from   denglu   where   dengluNO=   "   &   id
                Dim   conn   As   New   SqlConnection
                conn.ConnectionString   =   "data   source=192.168.31.199;initial   catalog=kaoqin   manager;user   id=james5;password=james5; "
                Dim   cmd   As   New   SqlCommand(sql,   conn)
                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()
                Call   linker()
        End   Sub
肯请大家帮忙解决

------解决方案--------------------
你前台代码是不是定义了DataKeyField

后台是不是
int id = int.Parse(GridView1.DataKeys(e.RowIndex).ToString());
------解决方案--------------------
Dim id As Integer = GridView1.DataKeys(e.RowIndex).Value
感觉问题应该出在这句上
其实你直接用e.keys(i).tostring就好了,记住要定义gridview 的datakeynames