Compartir a través de


How to: Create Lookup Tables in Windows Forms Applications

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.

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.

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 would expand the Orders table in the Data Sources window and set the main node to Details, set the CustomerID column to use a ComboBox (or any other control that supports lookup binding), and drag the Orders node onto your form. Then you would 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. For more information, see How to: Display Related Data in a Windows Forms Application.

  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 How to: 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 example above) and 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

Tasks

Walkthrough: Creating a Lookup Table in a Windows Forms Application

Walkthrough: Creating a Windows Forms User Control that Supports Lookup Databinding

How to: Create a Lookup Table for a Windows Forms ComboBox, ListBox, or CheckedListBox Control

How to: Create a Lookup Table with the Windows Forms BindingSource Component

Concepts

Binding Windows Forms Controls to Data in Visual Studio

Data Sources Overview

TableAdapter Overview

Other Resources

Data Walkthroughs