Putting a gridview row in edit mode programmatically

How do I put a gridview row in edit mode programmatically?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Set the EditIndex property to the appropriate row and then ReBind the GridVIew again to it’s DataSource.

Hope this helps.

Method 2

Just implement the Row_Editing event and do something like this:

protected void Row_Editing(object sender, GridViewEditArgs e) 
  myGridView.EditItemIndex = e.EditItemIndex; 

Bind data will populate the GridView with the data.

Method 3

You may also need to know how to cancel the edit. Just like you set up the “OnRowEditing” command in the gridview, you need to set up the “OnRowCancelingEdit” command. The backend should look similar to this. (VB)

Sub gridView1_rowCanceling(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
    gridView1.EditIndex = -1
    BindData()  // <-- Whatever procedure you use to bind your data to the gridView
End Sub

Method 4

protected void gridview_RowEditing(object sender, GridViewEditEventArgs e)
    GridView gv = (GridView)sender;
    // Change the row state
    gv.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;           

Method 5

protected void btnEdit_Click(object sender, EventArgs e)
    GridView1.EditIndex = 1;

Tested with vs-2008. fork fine.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x