C# gridView.DataSource as DataTable 在 asp.net 中设置为 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11778817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:12:54  来源:igfitidea点击:

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

c#asp.netgridviewdatasource

提问by DNR

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

我将我的 gridview.datasource 设置为一个数据表变量,如下所示:

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?

gvPaymentHistory.DataSource 有一条记录,但是,在该行执行后 dt 为空。如何将数据源记录传递给 dt?

EDIT

编辑

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

DataSource 是类对象的 List 集合。它不是数据集

采纳答案by Sravan Kumar

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.

希望这能解决您的问题。

回答by Pranay Rana

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;

回答by huMpty duMpty

Try This Way: Use BindingSource

试试这种方式:使用BindingSource

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