1 Previous Next 

DataGridView: Master Child for OleDB with 3 DataGridViews and creating relations for that


It seems easy to use a DataGridView in a relation. And that is true; if you know how to do it. Here is that code below. You need to drag three DataGridViews on your form and past this in. Also you have to change the place of your NorthWind sample file in the code to the way you use.

Untitled 4
Imports System.Data.OleDb
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object,
                  ByVal e As System.EventArgsHandles MyBase.Load
        Dim ds As DataSet
        Using conn = New OleDbConnection _
            ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test1\Nwind.mdb")
            ds = New DataSet
            Dim daEmployees = New OleDbDataAdapter("Select * from Employees", conn)
            Dim daOrders = New OleDbDataAdapter("Select * from Orders", conn)
            Dim daOrderDetails = New OleDbDataAdapter("Select * from [Order Details]", conn)
            daEmployees.Fill(ds, "Employee")
            daOrders.Fill(ds, "Orders")
            daOrderDetails.Fill(ds, "OrderDetails")
        End Using
        ds.Relations.Add("EmployeeOrder"ds.Tables("Employee").Columns("EmployeeID"), _
            ds.Tables("Orders").Columns("EmployeeID"))
        ds.Relations.Add("Order2Details"ds.Tables("Orders").Columns("OrderID"), _
            ds.Tables("OrderDetails").Columns("OrderID"))
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "Employee"
        DataGridView2.DataSource = ds
        DataGridView2.DataMember = "Employee.EmployeeOrder"
        DataGridView3.DataSource = ds
        DataGridView3.DataMember = "Employee.EmployeeOrder.Order2Details"
    End Sub
End Class



1 Previous Next