Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 406
DNR Avatar asked Aug 02 '12 13:08

DNR


1 Answers

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.

like image 60
Sravan Kumar Avatar answered Sep 18 '22 14:09

Sravan Kumar