Parent Child DataGridViews

DavePro1962

Member
Joined
Feb 5, 2010
Messages
6
Programming Experience
1-3
Hi All

Having a real problem with this this.

Can I have a 2 datagridview, one as a parent and one as a child, by that I mean if I click on a record in the parent datagridview it populars the child datagridview with all the child records from the parent.

I hope this makes sense, any help would be appreciated.

Regards

Dave
 
Yes you can

See the DW3 link in my signature, section "Displaying Related Data"

Note: it may advocate that you download ALL parent and ALL child rows
I usually prefer to down child rows only when the parent binding source changes position, and there are no child rows already, but it depends on the size of your datasets
 
Thanks but.........

Thanks for the response cjard.

However I was trying to do this in the code (if that makes sence).

But this is a good starting point.

Dave
 
While I would strongly suggest following cjard's advice you can manually set the the relationship and bindings like this:

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds As DataSet = Me.GetDataSet()

        Dim masterBs As New BindingSource
        masterBs.DataSource = ds
        masterBs.DataMember = "Master"

        Dim detailsBs As New BindingSource
        detailsBs.DataSource = masterBs
        detailsBs.DataMember = "MasterDetails"

        dgvMaster.DataSource = masterBs
        dgvDetails.DataSource = detailsBs

    End Sub

    Private Function GetDataSet() As DataSet
        Dim ds As New DataSet
        Dim master As DataTable = Me.GetMasterDataTable()
        Dim details As DataTable = Me.GetDetailsDataTable()

        ds.Tables.Add(master)
        ds.Tables.Add(details)

        ds.Relations.Add("MasterDetails", master.Columns("ID"), details.Columns("MasterID"))

        Return ds
    End Function

    Private Function GetMasterDataTable() As DataTable
        Dim tbl As New DataTable("Master")
        With tbl.Columns
            .Add("ID", GetType(Integer))
            .Add("SomeMasterText", GetType(String))
        End With

        With tbl.Rows
            .Add(1, "Master 1")
            .Add(2, "Master 2")
            .Add(3, "Master 3")
        End With

        Return tbl
    End Function

    Private Function GetDetailsDataTable() As DataTable
        Dim tbl As New DataTable("Details")
        With tbl.Columns
            .Add("ID", GetType(Integer))
            .Add("MasterID", GetType(Integer))
            .Add("SomeChildText", GetType(String))
        End With

        With tbl.Rows
            .Add(1, 1, "Child 1")
            .Add(2, 1, "Child 2")
            .Add(3, 1, "Child 3")
            .Add(4, 2, "Child 4")
            .Add(5, 2, "Child 5")
            .Add(6, 2, "Child 6")
            .Add(7, 3, "Child 7")
            .Add(8, 3, "Child 8")
            .Add(9, 3, "Child 9")
        End With

        Return tbl
    End Function
End Class
 
Thanks MattP

Thanks Matt

The problem I have is I have about 15,000 master records and I do not want to fill the datagridview with all the records when I load the form.

I am happy to load the 15,000 records into a dataset and then enter a search criteria into a text box to return the relevant records from the dataset.

I don't seem to be able to do this when I build the relationship from the form designer, or am I missing something. It seems to me the all the code is generated in the "InitializeComponent()" of the form designer which it states you should not edit, so I am not sure how I can move the code into a Click event of a button.

I hope I have explained my self OK.

Thanks again.

Dave
 
Back
Top