Partager via


How to: Bind a Windows Forms ComboBox or ListBox Control to Data

You can bind the ComboBox and ListBox to data to perform tasks such as browsing data in a database, entering new data, or editing existing data.

To bind a ComboBox or ListBox control

  1. Set theDataSourceproperty to a data source object. Possible data sources include a BindingSource bound to data, a data table, a data view, a dataset, a data view manager, an array, or any class that implements the IList interface. For more information, see Data Sources Supported by Windows Forms.

  2. If you are binding to a table, set theDisplayMemberproperty to the name of a column in the data source.

    - or -

    If you are binding to an IList, set the display member to a public property of the type in the list.

    Private Sub BindComboBox()
      ComboBox1.DataSource = DataSet1.Tables("Suppliers")
      ComboBox1.DisplayMember = "ProductName"
    End Sub
    
    private void BindComboBox()
    {
      comboBox1.DataSource = dataSet1.Tables["Suppliers"];
      comboBox1.DisplayMember = "ProductName";
    }
    
    private void BindComboBox() 
    {
       comboBox1.set_DataSource(dataSet1.get_Tables().get_Item(
       "Suppliers"));
       comboBox1.set_DisplayMember("ProductName");
    }
    
    NoteNote

    If you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList, the bound control's data will not be updated when the data source is updated. For example, if you have a combo box bound to an ArrayList and data is added to the ArrayList, these new items will not appear in the combo box. However, you can force the combo box to be updated by calling the SuspendBinding and ResumeBinding methods on the instance of the BindingContext class to which the control is bound.

See Also

Reference

ComboBox
ListBox

Concepts

Data Binding and Windows Forms

Other Resources

Windows Forms Data Binding
Windows Forms Controls Used to List Options