Share via


Create lookup tables in Windows Forms applications

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

The term lookup table describes controls that are bound to two related data tables. These lookup controls display data from the first table based on a value selected in the second table.

You can create lookup tables by dragging the main node of a parent table (from the Data Sources Window) onto a control on your form that is already bound to the column in the related child table.

For example, consider a table of Orders in a sales database. Each record in the Orders table includes a CustomerID, indicating which customer placed the order. The CustomerID is a foreign key pointing to a customer record in the Customers table. In this scenario, you expand the Orders table in the Data Sources window and set the main node to Details. Then set the CustomerID column to use a ComboBox (or any other control that supports lookup binding), and drag the Orders node onto your form. Finally, drag the Customers node onto the control that is bound to the related column — in this case, the ComboBox bound to the CustomerID column.

To databind a lookup control

  1. Open the Data Sources window.

    Note

    Lookup tables require that two related tables or objects are available in the Data Sources window.

  2. Expand the nodes in the Data Sources window until you can see the parent table and all of its columns, and the related child table and all of its columns.

    Note

    The child table node is the node that appears as an expandable child node in the parent table.

  3. Change the drop type of the child table to Details by selecting Details from the control list on the child table's node. For more information, see Set the control to be created when dragging from the Data Sources window.

  4. Locate the node that relates the two tables (the CustomerID node in the previous example).Change its drop type to a ComboBox by selecting ComboBox from the control list.

  5. Drag the main child table node from the Data Sources window onto your form.

    Databound controls (with descriptive labels) and a tool strip (BindingNavigator) appear on the form. A DataSet, TableAdapter, BindingSource, and BindingNavigator appear in the component tray.

  6. Now drag the main parent table node from the Data Sources window directly onto the lookup control (the ComboBox).

    The lookup bindings are now established. Refer to the table below for the specific properties that were set on the control.

    Property Explanation of setting
    DataSource Visual Studio sets this property to the BindingSource created for the table you drag onto the control (as opposed to the BindingSource created when the control was created).

    If you need to make an adjustment, then set this to the BindingSource of the table with the column you want to display.
    DisplayMember Visual Studio sets this property to the first column after the primary key that has a string data type for the table you drag onto the control.

    If you need to make an adjustment, then set this to the column name you want to display.
    ValueMember Visual Studio sets this property to the first column participating in the primary key, or the first column in the table if no key is defined.

    If you need to make an adjustment, then set this to the primary key in the table with the column you want to display.
    SelectedValue Visual Studio sets this property to the original column dropped from the Data Sources window.

    If you need to make an adjustment, then set this to the foreign-key column in the related table.

See Also

Bind Windows Forms controls to data in Visual Studio