Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Windows Forms
 How to: Bind a Windows Forms ComboB...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Windows Forms Programming
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 the DataSource property 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 the DisplayMember property 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.

    Visual Basic
    Private Sub BindComboBox()
      ComboBox1.DataSource = DataSet1.Tables("Suppliers")
      ComboBox1.DisplayMember = "ProductName"
    End Sub
    
    

    C#
    private void BindComboBox()
    {
      comboBox1.DataSource = dataSet1.Tables["Suppliers"];
      comboBox1.DisplayMember = "ProductName";
    }
    
    

    J#
    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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker