本文介绍了如何投放bindingdatasource到数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用此代码的bindingdatasource转换为数据表
I'm trying to cast the bindingdatasource to datatable using this code
BindingSource bs = (BindingSource)gvSideMember.DataSource;
DataTable tCxC = (DataTable)bs.DataSource;
抛出错误无法施展BindingSource的数据表
throws error unable to cast bindingsource to datatable
然后我尝试这种代码
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = ((DataTable)dgv.DataSource).Copy();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (!column.Visible)
{
dt.Columns.Remove(column.Name);
}
}
return dt;
}
这再次显示了我同样的错误。
it again show me same error
DataTable dt = new DataTable();
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView dv = new DataView();
dv = (DataView)SqlDataSource1.Select(args);
dt = dv.ToTable();
但我不知道什么是基类DataSourceSelectArguments的?所以我不能我该怎么办这个转换?
but i don't know what is the base class of DataSourceSelectArguments ? So I can't how can i do this cast?
推荐答案
看起来像你的 bs.DataSource
实际上是另一个的BindingSource
,所以你可以尝试这样的:
Looks like your bs.DataSource
is actually another BindingSource
, so you can try this:
var source = bs.DataSource;
while(source is BindingSource){
source = ((BindingSource)source).DataSource;
}
if(source is DataTable){
var table = (DataTable) source;
}//else there is not any DataTable we can extract.
这篇关于如何投放bindingdatasource到数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!