views:

25

answers:

1

Hi

If we have the following code, then when user clicks an Edit button, page is posted back and put into Edit mode:

protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
    gvwEmployees.EditIndex = e.NewEditIndex;

    gvwEmployees.DataSource = ds.Tables["Employees"];

    gvwEmployees.DataBind();
}


But with the following code, user has to click the Edit button twice before a row is put into edit mode ( thus page needs to be posted back twice before row gets into edit mode). Why does it matter whether gvwEmployees.EditIndex is assigned a value before or after we bind GridView to a data source?

protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
    gvwEmployees.DataSource = ds.Tables["Employees"];

    gvwEmployees.DataBind();

    gvwEmployees.EditIndex = e.NewEditIndex;
}


Thank you

+1  A: 

Modifying the EditIndex property with a value different than the one it already has requires that DataBind() is called after the modification.

As described in the GridView.EditIndex documentation page, it could also happen if EditIndex is modified under other circumstances:

If you set the EditIndex property after a postback or in handlers for events that are raised later than the Load event, the GridView control might not enter edit mode for the specified row. If you read the value of this property in other event handlers, the index is not guaranteed to reflect the row that is being edited.

Gonzalo
I'm not entirelly sure how to ask this, but - Since calling DataBind retrieves all rows and not just the row with index equal to GridView.EditIndex, I would think GridView wouldn't have any problems to put any of the retrieved rows into edit mode, even if EditIndex is set after calling Databind?!
carewithl
I would think that you can modify `EditIndex` until the PreRender event is triggered. No idea why this does not work.
Gonzalo
thanx for helping me out
carewithl