views:

20

answers:

1

I've databound some data to a datagrid and I'm wanting to be able to get the original data back.

I've databound the data as follows in one section of my code.

IEnumerable<MyClass> myClasses = GetMyClassesFromDatabase();
DataGridForMyClass.DataSource = myClasses;
DataGridForMyClass.DataBind();

When I've clicked a button, I want to be able to do stuff with the items in the datagrid.

How do I, given the DataGridForMyClass, retrieve the original list of MyClass?

+1  A: 

You can't pull it back out of the grid. You will need to store the data somewhere (either Session, Cache, or ViewState) and retrieve it form there. Only the basic controls and their values are stored in the grid.

For example if you are displaying a DateTime in a grid that uses a Label, the DateTime is not stored, just the text representation that was put and persisted via the Label is actually in the grid, your original bound data is not.

EDIT: From your comment, if you want to edit data in the grid then you have to post the data back and use the RowCommand or RowUpdating event. See the following article:

http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

Kelsey
Then how do I persist the changes to those items, with a few modifications, back to the database?
mezoid
@mezoid if this answer helped you don't forget to mark it as the accepted answer. If you need more help let me know.
Kelsey
I ended up parsing out the data from the user controls into a data transfer object and then using that object to retrieve the records from the database and update the details. While your answer doesn't exactly match what I did I guess it did point me in the direction of my chosen solution so I guess I can mark this one an answered.
mezoid