Category / Section
How to bind generic list with the ComboBoxAdv?
1 min read
You can bind the ComboBoxAdv with Generic List. Refer to the following steps to achieve this requirement.
1)Initialize the Class and the Properties based on your requirement.
2)Populate the List with required details.
3)Proceed to assign as ComboBoxAdv DataSource.
The following code example demonstrates the same.
C#
List<Country> CountryList = new List<Country>(); //Adds the item into the list CountryList.Add(new Country { Key = "India" }); CountryList.Add(new Country { Key = "US" }); CountryList.Add(new Country { Key = "UK" }); CountryList.Add(new Country { Key = "China" }); //Binds the list to the ComboBoxAdv comboBoxAdv1.DataSource = CountryList; comboBoxAdv1.DisplayMember = "Key"; private void comboBoxAdv1_SelectedIndexChanged(object sender, EventArgs e) { this.listBox1.Items.Clear(); //Gets the ComboBoxAdv SelectedItem Country selectedCountry = comboBoxAdv1.SelectedItem as Country; this.listBox1.Items.Add(selectedCountry.Key); } //Specifies the List public class Country { public string Key {get; set;} }
VB
Dim CountryList As New List(Of Country)() 'Adds the item into the list CountryList.Add(New Country With {.Key = "India"}) CountryList.Add(New Country With {.Key = "US"}) CountryList.Add(New Country With {.Key = "UK"}) CountryList.Add(New Country With {.Key = "China"}) 'Binds the list to the ComboBoxAdv comboBoxAdv1.DataSource = CountryList comboBoxAdv1.DisplayMember = "Key" Private Sub comboBoxAdv1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBoxAdv1.SelectedIndexChanged Me.listBox1.Items.Clear() 'Gets the ComboBoxAdv SelectedItem Dim selectedCountry As Country = TryCast(comboBoxAdv1.SelectedItem, Country) Me.listBox1.Items.Add(selectedCountry.Key) End Sub 'Specifies the List Public Class Country Private privateKey As String Public Property Key() As String Get Return privateKey End Get Set(ByVal value As String) privateKey = value End Set End Property End Class
Figure 1:The selected item of the ComboBoxAdv is displayed in ListBox.
Sample Links:
C#: https://www.syncfusion.com/downloads/support/directtrac/141940/ze/ComboBoxList-1025859467
VB: https://www.syncfusion.com/downloads/support/directtrac/141940/ze/ComboBoxList_VB-587598701