Reputation: 341
I was searching how to change mode of gridview row to Edit mode.. And, I got one answer,
Gridview1.EditIndex =1;
So, I put that into Rowcommand event. I thought it simply change mode of first row regard less pressing Edit button anywhere from any row. But surprisingly, its just changing row mode, where edit button it pressed. Can anyone tell me how does this work?
I am clicking Edit button, but it is custom Link Button, which has CommandName="Edit", and then I put this code in my .cs file
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridView1.EditIndex = 2; // tested in VS 2008, .NET 3.5
// Here doesn't matter, if I write 1,2 or any number, but when I click Edit button from row, then same row only go into Edit mode.
}
------
}
Upvotes: 1
Views: 10450
Reputation: 10565
When you want to put your GridView in Edit mode programmatically,set the EditIndex
property to the appropriate row.
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
NOTE:
1.) In case you are binding gridview from markup, using DataSourceControls such as SqlDataSource
, No Need to call the GridView.Bind()
.
2.) If you are binding GridView at runtime using the DataSource property, you should rebind your gridView.
In your Question: surprisingly, its just changing row mode, where edit button is pressed.
, since you are clicking the Edit
button, handle the OnRowEditing
event and set the NewEditIndex
property, if you want some other row to be in edit mode::
<asp:GridView ID="CustomersGrIdView"
OnRowEditing="CustomersGridView_Editing"
OnRowCommand="CustomersGridView_RowCommand"
.... />
protected void CustomersGridView_Editing(object sender, GridViewEditEventArgs e)
{
e.NewEditIndex = 3; // Tested in VS 2010, Framework 4
}
Now regardless of any row's Edit
button clicked, you will always find, the 3rd row in Edit mode. [ Row Numbering starts from 0 ]
This is what MSDN has to say about EditIndex
property. Most of the time, we use the Option C.
This property is typically used only in the following scenarios, which involve handlers for specific events:
a. You want the GridView control to open in edit mode for a specific row
the first time that the page is displayed. To do this, you can set the
EditIndex property in the handler for the Load event of the Page class
or of the GridView control.
b. You want to know which row was edited after the row was updated.
To do this, you can retrieve the row index from the EditIndex property
in the RowUpdated event handler.
c. You are binding the GridView control to a data source by setting the
DataSource property programmatically. In this case you must set the
EditIndex property in the RowEditing and RowCancelingEdit event handlers.
Upvotes: 1