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?
You can’t pull it back out of the grid. You will need to store the data somewhere (either
Session,Cache, orViewState) and retrieve it form there. Only the basic controls and their values are stored in the grid.For example if you are displaying a
DateTimein a grid that uses aLabel, the DateTime is not stored, just the text representation that was put and persisted via theLabelis 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
RowCommandorRowUpdatingevent. See the following article:http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx