does gridview save data in viewstate?

hi
i bind a DataTable to a gridview.
i want to use the DataTable in some postback event, and ii want to use viewstate.
does gridview save data in viewstate? if no what is the best way to do this.

(full story :
i have a custom gridview derived from standard asp.net gridview,
and i put a button in header to export data to excel, i want to use data saved in viewstate to export to excel)

thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The short answer is yes, information shown in the GridView, much like the current state of any control, is stored in ViewState. However, I do not think it is easy, if even possible, to get to this data yourself out of the ViewState collection that is available to WebControls. Instead, ASP.NET populates values from ViewState before running event handlers in the lifecycle, so if you interrogate your GridView object server-side in a handler, you should see the current values of the cells.

Method 2

There are other ways like:

1: Storing your DataTable in Session

2: Depending on how often that export button is going to be used and the amount of data fetched, you can make a DB call.

Method 3

Below Statement would make the idea clear:

It Clearly shows that gridview data is stored in viewstate:

//Binding GridView
gv_Settlements.DataSource = dt_settlements;
gv_Settlements.DataBind();

//Retrieving Data
DataTable dt = (DataTable)gv_Settlements.DataSource;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x