Tutorials
ODP.NETAs mentioned before, a DataSet object can have its own relations between data tables existing in it. We can add these relations dynamically at the client side (within an application), to represent master-detail (or hierarchical) information. The following code gives the list of employees (in the bottom grid) based on the department you choose in the top grid:
Once the DataSet is filled with data tables (Departments and Employees), we can add an in-memory relation using the following statement:
ds.Relations.Add(New DataRelation("FK_Emp_Dept",
ds.Tables("Departments").Columns("Deptno"),
ds.Tables("Employees").Columns("Deptno")))
The above statement simply adds a new relation (named FK_Emp_Dept) between two DataTable objects (Departments and Employees) based on the column Deptno (available in both DataTable objects).
To present the information in a master-detail fashion, we can make use of the BindingSource object as follows:
Dim bsMaster As New BindingSource(ds, "Departments")
Dim bsChild As New BindingSource(bsMaster, "FK_Emp_Dept")
In the above code fragment, we used two BindingSource objects corresponding to master and child data tables respectively. The child BindingSource object is created based on the master BindingSource object together with the specification of DataRelation. Once the BindingSource objects are ready, we can assign them as data sources to the DataGridView controls as following:
Me.DataGridView1.DataSource = bsMaster
Me.DataGridView2.DataSource = bsChild
The output for the above code would look similar to the following figure:

You can observe that this screen displays only the employees working in department number 20 as that is selected in the top grid.