VB.Net : How do you bind a dictionary collection to a Combobox?

泪湿孤枕 提交于 2021-02-08 06:47:06

问题


I am trying to bind this dictionary collection to a combobox but the display is not correct. The displayMember should be the ProvName and the ValueMember should be the key.

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click

    Dim Country1 As Dictionary(Of String, Province)

    Country1 = Module1.CreateCountry

    'Display results in combox
    ComboBox3.DataSource = New BindingSource(Country1, Nothing)
    ComboBox2.DisplayMember = "Value"
    ComboBox2.ValueMember = "Key"



End Sub

Module Module1 Public provinces As CollectionBase

Function CreateCountry() As Dictionary(Of String, Province)

    Dim Country As New Dictionary(Of String, Province)

    Dim Prov As Province

    Prov = New Province
    With Prov
        .Abbrv = "Qc"
        .ProvName = "Quebec"
        .Population = "7 500 000"
        .Region = "East"
    End With
    Country.Add(Prov.Abbrv, Prov)

    Prov = New Province
    With Prov
        .Abbrv = "BC"
        .ProvName = "British Columbia"
        .Population = "4 500 000"
        .Region = "West"
    End With
    Country.Add(Prov.Abbrv, Prov)

    Prov = New Province
    With Prov
        .Abbrv = "NS"
        .ProvName = "Nova Scotia"
        .Population = "2 000 000"
        .Region = "Maritimes"
    End With
    Country.Add(Prov.Abbrv, Prov)

    Prov = New Province
    With Prov
        .Abbrv = "AB"
        .ProvName = "Alberta"
        .Population = "5 500 000"
        .Region = "Prairies"
    End With
    Country.Add(Prov.Abbrv, Prov)



    Return Country

End Function

End Module

Public Class Province
  Public Property Abbrv As String
  Public Property ProvName As String
  Public Property Population As String
  Public Property Region As String

  Public Overrides Function ToString() As String
    Return ProvName
  End Function

End Class

回答1:


Here is the sample source code:

 'Declare and Fill a generic Dictionary

    Dim dictionary As New Dictionary(Of String, Integer)
    dictionary.Add("one", 1)
    dictionary.Add("two", 2)
    dictionary.Add("three", 3)
    dictionary.Add("four", 4)
    dictionary.Add("five", 5)
    dictionary.Add("six", 6)
    dictionary.Add("seven", 7)
    dictionary.Add("eight", 8)

  'Initialize DisplayMember and ValueMember of an existing combobox to be filled with dictionary values

                    cboCombo.DisplayMember = "Key"
                    cboCombo.ValueMember = "Value"

'Bind the combobox to dictionary

                    cboCombo.DataSource = New BindingSource(dictionary, Nothing)

 'Now I can assign the selected value of combobox with this simple command:

                   cboCombo.SelectedValue = 4

'I can also retrive the selected value with: value = cboCombo.SelectedValue

If this help you mark as answer




回答2:


This is just an example of using a dictionary to bind data to a combobox.

Declare dictionary object and initialize it with values as follows:

Dim dcItems As New Dictionary(Of String, Integer)
lstTPAType.Add("Select", -1)
lstTPAType.Add("Item 1", 0)
lstTPAType.Add("Item 2", 1)
lstTPAType.Add("Item ", 2)

cmbMyCombo.DataSource = New BindingSource(dcItems, Nothing)
cmbMyCombo.ValueMember = "Value"
cmbMyCombo.DisplayMember = "Key"

Hope this generic solution will help developers looking for it.



来源:https://stackoverflow.com/questions/35540272/vb-net-how-do-you-bind-a-dictionary-collection-to-a-combobox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!