Code snippet for binding ComboBox with arraylist in .net

The following example shows that how can you the bind combobox control from array list. ArrayList is the collection of the items. Right now we create the an ArrayList object of the string types values.

Example

The following code requires one combobox control with name ComboBox1.
[VB.Net>

    Private Sub BindComboBox()
        DIM Countrylist As New ArrayList
        With Countrylist
            .Add("India")
            .Add("USA")
            .Add("UK")
            .Add("Russia")
            .Add("China")
            .Add("Japan")
            .Add("Australia")
        End With
 
        ComboBox1.DataSource = Countrylist
        ComboBox1.SelectedIndex = 0
    End Sub

[C#.Net]

 private void BindComboBox()
        {
            ArrayList Countrylist = new ArrayList();
 
            Countrylist.Add("India");
            Countrylist.Add("USA");
            Countrylist.Add("UK");
            Countrylist.Add("Russia");
            Countrylist.Add("China");
            Countrylist.Add("Japan");
            Countrylist.Add("Australia");
 
            comboBox1.DataSource = Countrylist;
            comboBox1.SelectedIndex = 0;
        }

From the above, we use the DataSource property to bind the items from the ArrayList. When the DataSource property is set, the items collection cannot be modified.