【问题标题】:Populate multiple comboboxes from 1 ienumerable从 1 个 ienumerable 填充多个组合框
【发布时间】:2015-12-30 13:58:57
【问题描述】:

下午好,

有没有办法划分一个 ienumerable 并将所有不同的值绑定到不同的组合框。

如下图所示: 我有 10 个组合框,而这些的输入都来自 1 个可枚举的。

我确实可以选择为每个数据库做一个并遍历整个数据库,然后将它们添加到组合框中:

        Dim ts As IEnumerable(Of tblLabel)
        For Each itms In ts
            CmbDescription.Items.Add(itms.Description)
            CmbLabelId.Items.Add(itms.LabelID)
            ...
        Next

但我想知道是否可以将 Ienumerable 的不同“列”直接链接到相关组合框的数据源。 我正在寻找类似的选项:

         CmbDescription.DataSource = ts.Description
         CmbLabelId.DataSource = ts.LabelId
         ...

可悲的是,据我所知,这个 ienumerable 不能像这样拆分。 另一种解决方法是为所有这些组合框创建所有单独的 ienumerables,但这样代码太多了。

有什么想法吗?

【问题讨论】:

  • 为所有这些组合框创建所有单独的 ienumerables,但是代码太多 - 这正是您需要做的。每个ComboBox 都必须有自己的DataSource,在您的情况下,这是分隔的项目集合
  • 加入@Fabio 的 cmets。另请注意,此类组合框通常不包含重复项,因此您通常会将每个组合数据源设置为 enumerable.Select({field}).Distinct().ToList()
  • 您可以将每个组合框的数据源设置为表格,并设置每个组合框的datavalue和datatext字段,然后它将只抓取列表中的那些字段。
  • @MaCron,当在不同的组合框中使用相同的源/集合时,在一个组合框中更改 SelectedValue 也会影响其他组合框。
  • 不,它不会,每个控件都有自己的事件以及自己的文本和值。设置 DataSource 并绑定控件后,它们就可以使用了。

标签: vb.net linq combobox ienumerable


【解决方案1】:

我认为你原来的方法已经足够好了。
但是,如果您想使用 DataSource 属性通过单独的项目集合来填充 ComboBoxes,那么您可以简单地从 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()

【讨论】:

  • 感谢您迄今为止的所有意见。 @Fabio:我在尝试您的代码时收到一条错误消息。“System.ArgumentNullException:值不能为空。参数名称:System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable1 source, TAccumulate seed, Func3 func)
  • 您的ts As IEnumerable(Of tblLabel)null/Nothing。检查您的查询是否返回某些内容或至少返回空结果
  • 你是对的。在我对多个 ienumerables 进行返工期间,这被删除了。谢谢你的帮助,我一定会用这个!
【解决方案2】:

在不将每个数据源放入每个ComboBox 的情况下实现此目的的一种方法是实现DataGridView.Columns 中的列名和组合框名称ComboBox.Name 之间的映射。

这可以通过使用Dictionary 来完成,因此对于每个列名,您映射到特定的ComboBox。然后,您可以通过 foreach 或 for 循环进行填充。

然而,在某些情况下,您可能仍然希望每个 ComboBox 都有自己的数据源

【讨论】:

    猜你喜欢
    • 2020-03-14
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多