如何将 bindingdatasource 转换为数据表? [英] How to cast bindingdatasource to datatable?

查看:23
本文介绍了如何将 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 转换为数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆