Articles in this section
Category / Section

How to Bind Generic List with the WinForms ComboBox?

4 mins read

You can bind the WinForms ComboBox 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

 

Showing selected item of ComboBoxAdv displayed in ListBox

Figure 1:The selected item of the ComboBoxAdv is displayed in ListBox.


Conclusion

I hope you enjoyed learning about how to bind generic list with the WinForms ComboBox.

You can refer to our WinForms ComboBox feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied