我使用 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] + ..
最佳答案
DataSource
是 object
。
因此,您需要更多地了解您正在使用的实际 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/