views:

902

answers:

2

How can I get the underlying DataItem from a GridView row that is in edit mode and the user clicks on "Update"?

I have an editable GridView. I've added a CustomValidator to the Edit view of one of the columns. When the user clicks on Update I run a server validate method and need to get the underlying DataItem of the row being edited so that I can get the primary key of the data item for use in the validation.

GridView1.Rows[GridView1.EditIndex].DataItem;

This does not work. I guess because the GridView isn't databinded on the update command?

+2  A: 
GridView1.DataKeys[GridView1.EditIndex].Value;
Kosta
That is perfectly correct. Thank you very much!
metanaito
A: 

I had a similar problem. I use the rowdatabound event instead of the update event of the gridview.

Here is an example

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem%28VS.80%29.aspx#

Esteban Miccio