简体   繁体   中英

How to get data from a gridview at runtime on web form asp.net

I have a gridview on an aspx page.

the gridview already has a datasource and is populated.

i am trying to get the data out of it like this when the user clicks a button:

DataTable dt = (DataTable)grdList.DataSource;

however it is showing this result as null!

i understand this to be an issue with postback/viewstate.

can someone please recommend to me a solution by which i can get data out of the gridview?>

No you cannot get the data from asp.net gridview. Instead use this code.

Datatable dt = SomeMethodReturningDataTable();

Viewstate["Table"] = dt;

GridView.DataSource = ViewState["Table"];
Gridview.DataBind();

If you want to make chnages, pull out the table from viewstate and make changes and then push it again and bind it to grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM