1 Previous Next 

DataGrid: Master Detail in one DataGrid (Using Binding to a DataRelation) (SQLClient)


Microsoft has not direct from the box a newer DataGrid that can show relational datatables in one Grid. For that the Windows Forms DataGrid can still be used in Framework 4.

You have than to add it to your toolbox.

This sample uses the NorthWind SQL server sample. After draging a Datagrid on the form , pasting in this code below and changing in the code below the Server name for yours.

Untitled 4
Imports System.Data.SqlClient
Public Class Form1
    'VB10SP1 style so watch the byval  
    Private Sub Form1_Load(sender As System.Object, _
    e As System.EventArgsHandles MyBase.Load
        Dim ds As New DataSet
        Dim conn As New SqlConnection("Server = " & "Your Server" & _
           "; Database = NorthWind; " & _
           "Integrated Security  = sspi;")
        'Replace in Your Server your servername or Ip address
        Dim daEmployees As New SqlDataAdapter("Select * from Employees", conn)
        Dim daOrders As New SqlDataAdapter("Select * from Orders", conn)
        Dim daOrderDetails As New 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"))
        DataGrid1.ReadOnly = True
        DataGrid1.DataSource = ds
        DataGrid1.Expand(-1)
    End Sub
End Class



1 Previous Next