Alex Gordon
Alex Gordon

Reputation: 60751

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?>

Upvotes: 2

Views: 3668

Answers (1)

user240141
user240141

Reputation:

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.

Upvotes: 4

Related Questions