Solved

Set Gridview.datasource to a datatable

Posted on 2010-11-15
12
1,276 Views
Last Modified: 2012-05-10
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
Comment
Question by:CipherIS
  • 6
  • 5
12 Comments
 
LVL 6

Expert Comment

by:ajb2222
ID: 34136520
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34136572
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34136875
how do i set viewstate to the gridview?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34136909
We need to see what was originally assigned to this.gvNotification.DataSource?

// Show the line of code where
this.gvNotification.DataSource = "What";
0
 
LVL 1

Author Comment

by:CipherIS
ID: 34136963
this.gvNotification.Datasource = Datatable.

The datatable was populated with data from a Database.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 34137003
DataTable dt = Notification.GetNotifications();  //Returns DataTable
this.gvNotification.DataSource = dt;
this.gvNotification.DataBind();
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34137067
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34137084
Check to make sure that dt did in fact return results.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 34137142
The database did return data as the grid displays results.
datatable.jpg
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34137357
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
 
LVL 1

Author Comment

by:CipherIS
ID: 34137381
When I get to this point in the code

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

dtGrid is Null.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 34137498
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 Shortcut (VB) 4 34
Need to pass stored proc parameters in list 13 32
Error on Add method 1 37
Point to Current Row Ater Refresh Datagridview 3 20
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now