protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ss=0;
            ss = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            string Dsql = "delete from Admin where AdminID =" +ss+ "";
            SqlConnection conn = new SqlConnection(s);
            SqlCommand cmd = new SqlCommand(Dsql, conn);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('删除成功!');</script>");
                Response.Redirect("WebForm1.aspx");
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('删除失败!');</script>");
            }
            finally
            {
                conn.Close();
            }请问一下 我到底哪里错了 为什么说我索引超出范围呢?

解决方案 »

  1.   

    - - 原来是没在前台GridView 里面写上 DatakeyNames 写上我要删除的字段
      

  2.   

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
      int index=e.RowIndex ;
      GridViewRow gvr=GridView1.Rows[index];
      str = gvr.Cells[1].Text;
      int id=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value));}
    DataKeys是否定义
      

  3.   

     this.GridView1.DataKeyNames = new string[] { "ID" };
      

  4.   

    在gridview属性中,设置一下DatakeyNames,就可以了
      

  5.   

    GridView1.DataKeys[e.RowIndex]肯定是这里 看看RowIndex的值 或者 DataKyes定义了没