c# - 在 BindingSource 中组合多个列表

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

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

我想要做的是将 BindingSource.List 属性中的所有 DataGridView 组合成一个,这样我就可以使用 DataGridView.DataSource = BindingSource

我尝试了以下方法,但它只绑定(bind)集合本身,而不绑定(bind) .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] + ..

最佳答案

DataSourceobject

因此,您需要更多地了解您正在使用的实际 DS;那么你可以(也许使用合适的 Cast<> ) Concat (或 Union )各种枚举。

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

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

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

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

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

关于c# - 在 BindingSource 中组合多个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51524189/

相关文章:

C# 比较两个对象值

c# - 如何将事件处理程序委托(delegate)转换为具有不同签名的委托(delegate)

c# - 可以使用 FileZilla 或 WinSCP 连接到 FTP,但不能使用 FtpWebRequest 或 FluentFTP

c# - 如何在我的 WinForm 应用程序中调试此 StackOverflowException?

c# - 在 Datagridview 中分组

c# - 检测歌曲中的节拍

c# - 将实例添加到 MEF 容器

.net - 从 IValueConverter 获取对 ViewModel 的引用

WPF (.net 3.5) ValidationRule , IDataErrorInfo

WPF数据绑定(bind)线程安全?