【问题标题】:Combine multiple lists inside a BindingSource在 BindingSource 中合并多个列表
【发布时间】:2018-07-25 16:59:18
【问题描述】:

我正在使用BindingSource 在多个数据库调用中收集一些DataGridViews。

我想要做的是将BindingSource 的.List 属性中的所有DataGridViews 合并为一个,以便我可以使用DataGridView.DataSource = BindingSource。

我尝试了以下方法,但它只绑定集合本身,而不是 .List 属性。

BindingSource _bindingSource = new BindingSource();
...
...
while(<doing database calls>) {
    // Populate _DataGridView with some a DB Call

    _bindingSource.Add(_DataGridView); // Add _DataGridView to the _bindingSource
}
DataGrid.Datasource = _bindingSource;

我想要的是这样的

// Populate _bindingSource with the .List property/or all the items within _bindingSource 
DataGrid.Datasource = _bindingSource[0] + _bindingSource[1] + ..

【问题讨论】:

    标签: c# winforms data-binding datagridview odp.net


    【解决方案1】:

    DataSource 是 object。

    因此,您需要了解更多您正在使用的实际 DS;然后你可以(也许使用合适的Cast&lt;&gt;)Concat(或Union)各种枚举。

    让我们看一个使用绑定到DataTables 的DataGridViews 的简单示例:

    var twoDataSources = ((DataTable)dataGridView1.DataSource).Select()
                 .Concat(((DataTable)dataGridView2.DataSource).Select());
    
    var twoDataSources = ((DataTable)dataGridView1.DataSource).Select()
                  .Union(((DataTable)dataGridView2.DataSource).Select());
    

    第一个示例包含每个表中的每一行;秒包含所有唯一的行。

    如果您需要更多帮助,请添加创建数据源的实际代码。 (当然它们必须是现场兼容的..)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多