views:

145

answers:

1

hi

1) If we first set DropDownList.SelectedIndex to some value and then rebind control to data source, it’s SelectedIndex property will be reset to default. So why doesn’t something similar happen with GridView.SelectedIndex and GridView.EditIndex? It seems that unlike DropDownList, GridView doesn’t reset these two properties ( to their default values ) after a re-bind.


2)

If you set the GridView.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.

Why could setting GridView.EditIndex after postback or in handlers raised after Load event cause GridView not to enter into Edit mode?


thank you

+1  A: 

In order for a GridView row to be in edit mode, the grid must be databound. So resetting EditIndex wouldn't make much sense. As for SelectedIndex... I imagine that is just simply a design decision.

As for number two... I think this text is misleading. Does it come from MSDN? I think what it is trying to say is "don't do this or allow it to happen:"

some EventHandler {
    grid.EditIndex = X;
    grid.DataBind();
    grid.EditIndex = Y;
}

You see? What row are we editing now?

[Edit]

This has everything to do with Question 2. You see, with the above code we are actually still editing row X. (but most likely, you will get a state error when you go to postback). Setting EditIndex itself does not put the GridView into edit mode... so your question is somewhat moot to begin with. What actually puts a row in edit mode is having this property set when the grid is databound. EditIndex really just tells the Render method which template to use: ItemTemplate or EditTemplate.

Like I said before, I believe the text you quoted is misleading. The important thing is knowing when the data binding happens. The text you quoted seems to assume that that is happening during Load.

Bryan
Q2 - "You see? What row are we editing now?" - I'm not sure I understand the point you're trying to make! We're now editing row Y, but what has that got to do with my second question?
carewithl
I apologize for replying so late, but I didn't notice your reply. Anyways, in case you find the time - 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 re-set after calling Databind - why would it be so difficult to put row Y ( instead of row X ) into edit mode ( here I'm reffering to the code you've posted above )?!
carewithl
Because the call to DataBind is what triggers Render. Once the Render event has happened, it doesn't get called again.
Bryan
thank you for your help
carewithl