• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1339
  • Last Modified:

Set Gridview.datasource to a datatable

I want to extract the data from a gridview to a datatable.  

I've tried both but receive errors.

DataTable tbl = this.gv.DataSource as DataTable;
DataTable dtGrid = (DataTable)this.gv.DataSource;

any ideas?
0
CipherIS
Asked:
CipherIS
  • 6
  • 5
1 Solution
 
ajb2222Commented:
This will only work if you originaly set the datasource to a datatable

So if at one point you have
datatable tbl
// fill your table
gv.DataSource = tbl

then you can use

DataTable dtGrid = (DataTable)gv.DataSource;
0
 
CipherISAuthor Commented:
I originally set the datasource to a datatable.

I've attached a snapshot.  The gridview has data but when i try to set the datasource to the datatable i receive a null.

Any ideas?
datatable.jpg
0
 
CipherISAuthor Commented:
how do i set viewstate to the gridview?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fernando SotoRetiredCommented:
We need to see what was originally assigned to this.gvNotification.DataSource?

// Show the line of code where
this.gvNotification.DataSource = "What";
0
 
CipherISAuthor Commented:
this.gvNotification.Datasource = Datatable.

The datatable was populated with data from a Database.
0
 
CipherISAuthor Commented:
DataTable dt = Notification.GetNotifications();  //Returns DataTable
this.gvNotification.DataSource = dt;
this.gvNotification.DataBind();
0
 
Fernando SotoRetiredCommented:
Well then from the looks of the screen shot the Datatable was NOT populated with any results from the database and when you assigned it to the this.gvNotification.DataSource it got assigned a null.

Does the data grid show any results? Or can you verify that the Datatable that was populated from the database did return results.
0
 
Fernando SotoRetiredCommented:
Check to make sure that dt did in fact return results.
0
 
CipherISAuthor Commented:
The database did return data as the grid displays results.
datatable.jpg
0
 
Fernando SotoRetiredCommented:
I see from your last post that this is a web application of which I have little knowledge of. Are you trying to do this :

DataTable dtGrid = (DataTable)this.gv.DataSource;

After a postback? If so did you retrieve the data table from a session state?

0
 
CipherISAuthor Commented:
When I get to this point in the code

DataTable dtGrid = (DataTable)this.gv.DataSource;

dtGrid is Null.
0
 
Fernando SotoRetiredCommented:
As I stated my knowledge of web apps is limited. But when you do this :

DataTable dtGrid = (DataTable)this.gv.DataSource

Is this after you display the data to the user? If so have you restored the data table back to the this.gv.DataSource from let say the session state? If not this.gv.DataSource is most likely is a null.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now