DNR
DNR

Reputation: 3736

gridView.DataSource as DataTable is setting a null in asp.net

I am setting my gridview.datasource into a datatable variable as below:

DataTable dt = gvPaymentHistory.DataSource as DataTable;  

The gvPaymentHistory.DataSource has a record, however, the dt is null after that line has executed. How can I pass the Datasource records to dt?

EDIT

DataSource is List collection of a class object. It's not a DataSet

Upvotes: 6

Views: 27689

Answers (3)

Sravan Kumar
Sravan Kumar

Reputation: 1457

Simple way is to store the data source of the grid in view source when u r binding the data to the grid and then retrieve it from the view source every time you need it.

Gridview.Datasource = yourdatasource;
ViewState["mydatasource"] = yourdatasource;

While retrieving

DataTable dt = ViewState["mydatasource"] as DataTable;

Hope this solves your problem.

Upvotes: 2

Pranay Rana
Pranay Rana

Reputation: 176886

EDIT

if the bidning type is list than try out

List<CodeEntity> data= gvPaymentHistory.DataSource as List<CodeEntity>;

or

   List<CodeEntity> codes = (List<CodeEntity>)gvPaymentHistory.DataSource; 

check this

if(gvPaymentHistory.DataSource is DataTable)
   DataTable dt = gvPaymentHistory.DataSource as DataTable;
if(gvPaymentHistory.DataSource is DataView )
    DataView dv = gvPaymentHistory.DataSource as DataView;

Upvotes: 0

huMpty duMpty
huMpty duMpty

Reputation: 14460

Try This Way: Use BindingSource

BindingSource bs = (BindingSource)gvPaymentHistory.DataSource;
DataTable dt =((YourDataSetType) (bs.DataSource)).Tables[0];

Upvotes: 0

Related Questions