1 Previous Next 

DataGridView: Master Detail grids (in this case 3) without the BindingSourceBinding (SQLClient)


This sample shows how to bind relations to a DataGridView. For the sample is used the SQLserver sample database NorthWind. It needs than only to drag 3 DataGridViews on a form and pasting this class in. The sample shows as well the difference between the DataGrid and the DataGridView in "AllowUserToAddRows". For the rest it is equal to a DataGrid. The DataGridView is from Net 2.0. Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conn As New SqlClient.SqlConnection("Server = " & "Server" & _
        "; Database = NorthWind; " & _
        "Integrated Security  = sspi;")
        'Replace in Server your servername or Ip address
        Dim ds As New DataSet
        Dim daEmployees As New SqlClient.SqlDataAdapter("Select * from Employees", conn)
        Dim daOrders As New SqlClient.SqlDataAdapter("Select * from Orders", conn)
        Dim daOrderDetails As New SqlClient.SqlDataAdapter("Select * from [Order Details]", conn)
        daEmployees.Fill(ds, "Employees")
        daOrders.Fill(ds, "Orders")
        daOrderDetails.Fill(ds, "OrderDetails")
        ds.Relations.Add("EmployeeOrder", ds.Tables("Employees").Columns("EmployeeID"), _
            ds.Tables("Orders").Columns("EmployeeID"))
        ds.Relations.Add("Order2Details", ds.Tables("Orders").Columns("OrderID"), _
            ds.Tables("OrderDetails").Columns("OrderID"))
        DataGridView1.ReadOnly = True
        DataGridView2.ReadOnly = True
        DataGridView3.ReadOnly = True
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.DataSource = ds
        DataGridView2.DataSource = ds
        DataGridView3.DataSource = ds
        DataGridView1.DataMember = "Employees"
        DataGridView2.DataMember = "Employees.EmployeeOrder"
        DataGridView3.DataMember = "Employees.EmployeeOrder.Order2Details"
    End Sub
End Class



1 Previous Next