Victor

Victor

  • NA
  • 7
  • 0

How to get DataKeys for all rows of GridView with paging

Jan 25 2010 2:51 PM
I inherited from GridView to make a custom control that allows the user to make changes to the data on every page without committing those changes to the DB immediately.  Only when the user clicks a "Save" button are the changes committed to the DB.
 

Since fields are re-loaded every time user changes page, changes would not survive the user changing pages.  I created a custom object to store user's changes.  This object holds the key names/values of the row the changes belong to, and the name/values of the controls that have been changed.  I use OnRowDataBound event to show the user changes where they apply.  I serialize and save the object with the user changes to and from the viewstate for the object.
 
My problem now is, how do I tell GridView to update a specific row, i.e. commit the changes to the DB, when all I have are the DataKey pairs for the row.  I can find the row number by looking at the GridView's current DataKeys, but that only works for visible rows!  If the user has changes on another page, they will not be saved because I can't find the row number using the DataKeys, and UpdateRow requires the row number.
 

I can't save the row number in my "changes" object and use that because if the data set changes cause the user filters the results in the GridView, then the row number in my "changes" object no longer matches up with the correct row in the GridView.
Please, if you could help I would appreciate it.

Answers (2)