3

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?

dtc
  • 10,136
  • 16
  • 78
  • 104

2 Answers2

7
GridView1.DataKeys[GridView1.EditIndex].Value;
Kosta
  • 518
  • 3
  • 7
0

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#