从 1 个 ienumerable 填充多个组合框
Populate multiple comboboxes from 1 ienumerable
下午好,
有没有办法划分一个 ienumerable 并将所有不同的值绑定到不同的组合框。
如下图所示:
我有 10 个组合框,而这些组合框的输入都来自 1 个 ienumerable。
我确实可以选择为每个人做一个并遍历整个数据库,然后将它们添加到组合框中:
Dim ts As IEnumerable(Of tblLabel)
For Each itms In ts
CmbDescription.Items.Add(itms.Description)
CmbLabelId.Items.Add(itms.LabelID)
...
Next
但我想知道我是否可以 link Ienumerable 的不同 'columns' 直接到关联组合框的数据源。
我正在寻找类似这样的选项:
CmbDescription.DataSource = ts.Description
CmbLabelId.DataSource = ts.LabelId
...
遗憾的是,据我所知,这个 ienumerable 不能像这样拆分。
另一种解决方法是为所有这些组合框创建所有单独的 ienumerables,但代码太多了。
有什么想法吗?
在不将每个数据源放入每个 ComboBox
的情况下实现此目的的一种方法是在 DataGridView.Columns
中的列名和组合框名称 ComboBox.Name
之间实现映射。
这可以通过使用 Dictionary
来完成,因此对于每个列名,您映射到特定的 ComboBox
。然后,您可以通过 foreach 或 for 循环进行填充。
然而,在某些情况下,您确实让每个 ComboBox 都有自己的数据源可能仍然更可取
我觉得你原来的方法已经足够好了。
但是,如果您想使用 DataSource
属性 通过单独的项目集合来填充组合框,那么您可以简单地从 IEnumerable
中获取所需的集合
CmbLabelId.DataSource =
ts.Select(function(label) label.LabelId).Distinct().ToList()
CmbDescription.DataSource =
ts.Select(function(label) label.Description).Distinct().ToList()
但是在这种方法中,您将循环 IEnumerable
的次数与您拥有的 ComboBoxes
的次数一样多。
这是我的方法,但还是想说你原来的方法很简单。
' In this class will be collected all distinct value of all columns
' Create own property for every column which used in the ComboBoxes
' With HashSet only distinct values will be collected (thanks to @Ivan Stoev's comment)
Public Class TblLabelProperties
Public Property LabelId As New HashSet(Of Integer)
Public Property Description As New HashSet(Of String)
' Other properties/columns
End Class
' Populate collections from database
Dim ts As IEnumerable(Of tblLabel)
Dim columnsValues As TblLabelProperties =
ts.Aggregate(New TblLabelProperties(),
Function(lists, label)
lists.LabelId.Add(label.LabelId)
lists.Description.Add(label.Description)
'Add other properties
Return lists
End Function)
' Set DataSources of comboboxes
CmbLabelId.DataSource = columnsValues.LabelId.ToList()
CmbDescription.DataSource = columnsValues.Description.ToList()
下午好,
有没有办法划分一个 ienumerable 并将所有不同的值绑定到不同的组合框。
如下图所示: 我有 10 个组合框,而这些组合框的输入都来自 1 个 ienumerable。
我确实可以选择为每个人做一个并遍历整个数据库,然后将它们添加到组合框中:
Dim ts As IEnumerable(Of tblLabel)
For Each itms In ts
CmbDescription.Items.Add(itms.Description)
CmbLabelId.Items.Add(itms.LabelID)
...
Next
但我想知道我是否可以 link Ienumerable 的不同 'columns' 直接到关联组合框的数据源。 我正在寻找类似这样的选项:
CmbDescription.DataSource = ts.Description
CmbLabelId.DataSource = ts.LabelId
...
遗憾的是,据我所知,这个 ienumerable 不能像这样拆分。 另一种解决方法是为所有这些组合框创建所有单独的 ienumerables,但代码太多了。
有什么想法吗?
在不将每个数据源放入每个 ComboBox
的情况下实现此目的的一种方法是在 DataGridView.Columns
中的列名和组合框名称 ComboBox.Name
之间实现映射。
这可以通过使用 Dictionary
来完成,因此对于每个列名,您映射到特定的 ComboBox
。然后,您可以通过 foreach 或 for 循环进行填充。
然而,在某些情况下,您确实让每个 ComboBox 都有自己的数据源可能仍然更可取
我觉得你原来的方法已经足够好了。
但是,如果您想使用 DataSource
属性 通过单独的项目集合来填充组合框,那么您可以简单地从 IEnumerable
CmbLabelId.DataSource =
ts.Select(function(label) label.LabelId).Distinct().ToList()
CmbDescription.DataSource =
ts.Select(function(label) label.Description).Distinct().ToList()
但是在这种方法中,您将循环 IEnumerable
的次数与您拥有的 ComboBoxes
的次数一样多。
这是我的方法,但还是想说你原来的方法很简单。
' In this class will be collected all distinct value of all columns
' Create own property for every column which used in the ComboBoxes
' With HashSet only distinct values will be collected (thanks to @Ivan Stoev's comment)
Public Class TblLabelProperties
Public Property LabelId As New HashSet(Of Integer)
Public Property Description As New HashSet(Of String)
' Other properties/columns
End Class
' Populate collections from database
Dim ts As IEnumerable(Of tblLabel)
Dim columnsValues As TblLabelProperties =
ts.Aggregate(New TblLabelProperties(),
Function(lists, label)
lists.LabelId.Add(label.LabelId)
lists.Description.Add(label.Description)
'Add other properties
Return lists
End Function)
' Set DataSources of comboboxes
CmbLabelId.DataSource = columnsValues.LabelId.ToList()
CmbDescription.DataSource = columnsValues.Description.ToList()