ASP .NET RowUpdating GridView Troubles

后端 未结 3 922
-上瘾入骨i
-上瘾入骨i 2021-01-21 11:46

I\'m having trouble with the RowUpdating Method. My GridView is connected to our local SQL Server, and I\'m trying to update the data. Here is the cod

相关标签:
3条回答
  • 2021-01-21 12:25

    try this code once.

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox tname = (TextBox)row.FindControl("nam");
        TextBox tques = (TextBox)row.FindControl("que");
        MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
        cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
        cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
        cmd.Parameters.Add("@ques", MySqlDbType.VarChar,40).Value = tques.Text.Trim();
        con.Open();
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        bind();
    }
    
    0 讨论(0)
  • 2021-01-21 12:25

    Not all GridViewRow have a DataItem.

    You should add a if block around your code and verify the row being updated is of type DataRow.

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         your code here...
    }
    

    More regarding RowTypes : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowtype.aspx

    0 讨论(0)
  • 2021-01-21 12:34
    public void bindGvEdit()
    {
        GridView1.DataSource = obj1.SelectAlltbl();
        GridView1.DataBind();
    }
    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindGvEdit();
    }
    
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindGvEdit();
    }
    
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        obj1.Id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
        obj1.Name = ((TextBox)row.Cells[1].Controls[1]).Text;
        obj1.Description = ((TextBox)row.Cells[2].Controls[1]).Text;
        obj1.Updatetbl();
        GridView1.EditIndex = -1;
        bindGvEdit();
    }
    
    0 讨论(0)
提交回复
热议问题