views:

601

answers:

1

looks like DataTable.Rows property is not saved in ViewState but the Columns property is, because I can't explain why columns persist between postbacks and the data doesn't. Any ideas?

+3  A: 

The DataTable class doesn't store anything at all in ViewState. The DataTable class is not specific for web applications and doesn't use any web specific features.

If any data from a DataTable object is stored in ViewState it's because you are using it as a data source for a web control that is storing information in ViewState, like for example a DataGrid. The control copies the information from the DataTable that it needs to recreate the control on postback. The DataTable object that was used to data bind the control initially doesn't exist any more once the page is sent to the browser.

The Columns property of the DataGrid control (for example) is a collection of DataGridColumn objects, it's not the same as the Columns property of the DataTable class that is a collection of DataColumn objects, eventhough most of the information from the columns of the DataTable is copied to the columns of the DataGrid when the DataGrid is data bound.

Guffa
But what I am seeing completely contradicts what you are saying. Note that I am not talking about GridView (I am aware that gridView does get stored in ViewState). WHat I am seeing is that Columns property of DataTable gets stored as well. i guess I will have to run few more tests.
gnomixa
my bad, you are right. I was adding columns earlier on and didn't notice.
gnomixa