Results 1 to 5 of 5

Thread: Parent Child DataGridViews

  1. #1
    DavePro1962 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Feb 2010
    Posts
    6
    Reputation
    0

    Parent Child DataGridViews

    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

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1724
    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

  3. #3
    DavePro1962 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Feb 2010
    Posts
    6
    Reputation
    0

    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

  4. #4
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    590
    While I would strongly suggest following cjard's advice you can manually set the the relationship and bindings like this:

    Code:
    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

  5. #5
    DavePro1962 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Feb 2010
    Posts
    6
    Reputation
    0

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking