Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Note
Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.
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
With your project open, open the Data Sources window by choosing View > Other Windows > Data Sources.
Note
Lookup tables require that two related tables or objects are available in the Data Sources window. For more information, see Relationships in datasets.
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.
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.
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.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.
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 following table 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, 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, 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, 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, set this to the foreign-key column in the related table.