Convert Grid View datasource to datatable

I ahve a grid view which has its inital bind done to a datatable.

I need to extract the datasource back into a datatable. When I try the following the datasource shows empty(Nothing)

 Dim dt As DataTable = DirectCast(gvSelect.DataSource, DataTable)


On my page load I get data using ADO.NET
and then assign the datatable as the datasource of the gridview
LVL 1
countrymeisterAsked:
Who is Participating?
 
CodeCruiserCommented:
I think in ASP.NET, you have to save the datatable to the Session to be able to use it after postback.
0
 
nudisoCommented:
I use C#....and use dataset to get table from database
And  DataTable can get tables from dataset easily.
      Datatable dt = dataset1.Table["Table_Name"];
About datadridView,
      DataTable dt1 = (DataTable)dataGridView1.Datasource;
0
 
AshokCommented:
Code you are using to convert to DataTable is correct.

When assigning DataTable to gvSelect, use

gvSelect.DataSource = dt
gvSelect.DataBind()

HTH
Ashok

0
 
countrymeisterAuthor Commented:
Ashok

I am already doing that when assigning a datosource to the datagrid.

I want to get back the datttable and the following does not work
Dim dt As DataTable = DirectCast(gvSelect.DataSource, DataTable)

In debug mode gvSelect.DataSource = Nothing
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.