GertDeWilde
GertDeWilde

Reputation: 351

Populate multiple comboboxes from 1 ienumerable

Good afternoon,

Is there a way to divide a ienumerable and bind all the different values to the different comboboxes.

Like you can see in the picture below: I got 10 comboboxes, while the input of these all come from 1 ienumerable.

Overview of the application

I do have an option to do a for each and go through the entire database, and add them to the combobox with:

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

But I wonder if I can link the different 'columns' of the Ienumerable directly to the datasource of the associated comboboxes. I'm searching for an option like:

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

Sadly enough, this ienumerable can't be splitsed like this, as far as I can see. Another workaround would be to create all separate ienumerables for all those comboboxes, but then it is too much code.

Any idea?

Upvotes: 0

Views: 327

Answers (2)

Fabio
Fabio

Reputation: 32445

I think your original approach is good enough.
But if you want populate ComboBoxes by separated collection of items using DataSource property, then you can simply get needed collection from IEnumerable

CmbLabelId.DataSource = 
    ts.Select(function(label) label.LabelId).Distinct().ToList()
CmbDescription.DataSource = 
    ts.Select(function(label) label.Description).Distinct().ToList()

But in this approach you will loop IEnumerable as much times as how much ComboBoxes you have.

Here is my approach, but again want to say that your original approach is simple enough.

' 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()

Upvotes: 2

Ian
Ian

Reputation: 30813

One way to implement this without putting each data source to each ComboBox is by implementing mapping between the column name in DataGridView.Columns and the combo box name ComboBox.Name.

This can be done by using Dictionary so for each column name, you map to specific ComboBox. Then, you can do the populating by foreach or for loop.

Yet, it might still be preferable in some cases that you really take each ComboBox having its own data source

Upvotes: 1

Related Questions