ODP.NET - Presenting Master-Detail Information Using a Dataset
As 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:
Imports Oracle.DataAccess.Client Public Class Form8 Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click 'create connection to db Dim cn As New OracleConnection("Data Source=xe; _ User Id=scott;Password=tiger") Try Dim ds As New DataSet Dim adp As OracleDataAdapter adp = New OracleDataAdapter("SELECT deptno, dname, loc FROM Dept", cn) adp.Fill(ds, "Departments") adp.Dispose() adp = New OracleDataAdapter("SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno FROM Emp", cn) adp.Fill(ds, "Employees") adp.Dispose() ds.Relations.Add(New DataRelation("FK_Emp_Dept", ds.Tables("Departments").Columns("Deptno"), ds.Tables("Employees").Columns("Deptno"))) Dim bsMaster As New BindingSource(ds, _ "Departments") Dim bsChild As New BindingSource(bsMaster, _ "FK_Emp_Dept") Me.DataGridView1.DataSource = bsMaster Me.DataGridView2.DataSource = bsChild Catch ex As Exception 'display if any error occurs MessageBox.Show("Error: " & ex.Message) 'close the connection if it is still open If cn.State = ConnectionState.Open Then cn.Close() End If End Try End Sub End Class
Imports%20Oracle.DataAccess.Client%20%0APublic%20Class%20Form8%20%0A%0A%20%20%20Private%20Sub%20btnData_Click%28ByVal%20sender%20As%20%0A%20%20%20System.Object%2C%20ByVal%20e%20As%20System.EventArgs%29%20Handles%20%0A%20%20%20btnData.Click%20%0A%20%20%20%20%20%27create%20connection%20to%20db%20%0A%20%20%20%20%20Dim%20cn%20As%20New%20OracleConnection%28%22Data%20Source%3Dxe%3B%20_%20%0A%20%20%20%20%20User%20Id%3Dscott%3BPassword%3Dtiger%22%29%20%0A%20%20%20%20%20Try%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20ds%20As%20New%20DataSet%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20adp%20As%20OracleDataAdapter%20%0A%0A%20%20%20%20%20%20%20%20%20%20adp%20%3D%20New%20OracleDataAdapter%28%22SELECT%20deptno%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dname%2C%20loc%20FROM%20Dept%22%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20adp.Fill%28ds%2C%20%22Departments%22%29%20%0A%20%20%20%20%20%20%20%20%20%20adp.Dispose%28%29%20%0A%0A%20%20%20%20%20%20%20%20%20%20adp%20%3D%20New%20OracleDataAdapter%28%22SELECT%20empno%2C%20ename%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20job%2C%20mgr%2C%20hiredate%2C%20sal%2C%20comm%2C%20deptno%20FROM%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Emp%22%2C%20cn%29%20%0A%20%20%20%20%20%20%20%20%20%20adp.Fill%28ds%2C%20%22Employees%22%29%20%0A%20%20%20%20%20%20%20%20%20%20adp.Dispose%28%29%20%0A%0A%20%20%20%20%20%20%20%20%20%20ds.Relations.Add%28New%20DataRelation%28%22FK_Emp_Dept%22%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ds.Tables%28%22Departments%22%29.Columns%28%22Deptno%22%29%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ds.Tables%28%22Employees%22%29.Columns%28%22Deptno%22%29%29%29%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20bsMaster%20As%20New%20BindingSource%28ds%2C%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Departments%22%29%20%0A%20%20%20%20%20%20%20%20%20%20Dim%20bsChild%20As%20New%20BindingSource%28bsMaster%2C%20_%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22FK_Emp_Dept%22%29%20%0A%20%20%20%20%20%20%20%20%20%20Me.DataGridView1.DataSource%20%3D%20bsMaster%20%0A%20%20%20%20%20%20%20%20%20%20Me.DataGridView2.DataSource%20%3D%20bsChild%20%0A%0A%20%20%20%20%20Catch%20ex%20As%20Exception%20%0A%20%20%20%20%20%20%20%20%20%20%27display%20if%20any%20error%20occurs%20%0A%20%20%20%20%20%20%20%20%20%20MessageBox.Show%28%22Error%3A%20%22%20%26amp%3B%20ex.Message%29%20%0A%20%20%20%20%20%20%20%20%20%20%27close%20the%20connection%20if%20it%20is%20still%20open%20%0A%20%20%20%20%20%20%20%20%20%20If%20cn.State%20%3D%20ConnectionState.Open%20Then%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cn.Close%28%29%20%0A%20%20%20%20%20%20%20%20%20%20End%20If%20%0A%20%20%20%20%20End%20Try%20%0A%20%20%20End%20Sub%20%0AEnd%20Class
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.
Trackback(0)
|