如题
怎么办 着急啊 各位帮帮忙 我上代码
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //--------------------------这个是在GridView上进行编辑事件处理
        GridView1.EditIndex = e.NewEditIndex;
        //bind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //--------------------------这个是在GridView上进行删除
        GridView1.DataKeyNames = new string[] { "id" };
        string aa = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string sql = "delete from stx_project_foreign where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        OracleCommand cmd = new OracleCommand(sql, DbConn.GetConn());
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
        //bind();
    }    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //--------------------------这个是在GridView上进行修改
        string sql = "update stx_project_foreign set 题目='"+((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        OracleCommand cmd = new OracleCommand(sql, DbConn.GetConn());
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
        GridView1.EditIndex = -1;
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //--------------------------这个是在GridView上进行取消操作
        GridView1.EditIndex = -1;
       // bind();
    }

解决方案 »

  1.   

    string aa = GridView1.DataKeys[e.RowIndex].Value.ToString();
    请看这里 我测试在这里出现了 错误
      

  2.   

    string aa = GridView1.DataKeys["id"].Value.ToString();
      

  3.   

    string aa =(强制转换成控件原类型) (GridView1.Items[e.RowIndex].findcontrl("存id的控件")).Text(或都value反正就是存值的那个属性。);
      

  4.   

    全在这儿了protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            bind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id;
            id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
            DataSet ds1 = new DataSet();
            string st1 = "delete from news where newid=" + id + "";
            bool inser = db.UpdataBySql(st1);
            if (inser == true)
            {
                //((LinkButton)e.RowIndex.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.RowIndex.Cells[2].Text + "\"吗?')");        }
            bind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox title;
            
            TextBox kind;
            TextBox href;
            TextBox date;
            TextBox time;
            int id=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            title = (TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
            kind = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
            href=(TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
            date = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
            time = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0];
            string ti = title.Text.Trim();
            string ki = kind.Text.Trim();
            string hr = href.Text.Trim();
            string da = date.Text.Trim();
            string tim=time.Text.Trim();
            DataSet ds2 = new DataSet();
            string str2 = "update news set title='"+ti+"',kind='"+ki+"',href='"+hr+"',dates='"+da+"',times='"+tim+"' where newid="+id+"";
            bool inser = db.UpdataBySql(str2);
            if (inser == true)
            {        }
            else { }
            GridView1.EditIndex = -1;
            bind();
        }
      

  5.   

    http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
      

  6.   

    还是不行 别拿 清清月儿 的七十二绝技混弄我  她写的是好 我就是照着她写的 但是我们绑定的方法不一样
    我现在就问 为什么出现
    "GridView在做行编辑的时候和删除的时候出现索引超出范围。必须为非负值并小于集合大小"
    这样的错误 多谢各位了
      

  7.   

            this.GridView1.DataKeyNames = new string[] { "ID" };//主键
            GridView1.DataBind();
            string aa = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sql = "delete from stx_project_foreign where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            OracleCommand cmd = new OracleCommand(sql, DbConn.GetConn());
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            GridView1.DataBind();
      

  8.   

     GridView1.DataKeyNames = new string[] { "id" };这个要在绑定之前执行的吧,你在要删除了才来定义key,太晚了
      

  9.   

    你看一下 e.rowindex.是多少就不行了
      错误提示已经说明白了。gridview中没有那一行。
      

  10.   

    在绑定的时候,加上这一句GridView1.DataKeyNames = new string[] { "id" };
    在GridView1.DataBind();之前
      

  11.   

    把GridView1.DataKeyNames = new string[] { "id" };
    放在 Page_Load()函数中定义,试一下。
      

  12.   

    "GridView1.DataKeyNames = new string[] { "id" };这个要在绑定之前执行的吧,你在要删除了才来定义key,太晚了"这个就是我想说的,你应该在页面中<asp:GridView DataKey="id"> ,这样就ok了
      

  13.   

    GridView1.DataKeyNames = new string[] { "ID" };//主键GridView1.Rows[row.RowIndex].FindControl() as TextBox;