Results 1 to 5 of 5

Thread: [RESOLVED] How do I create a many-to-many Relation?

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Resolved [RESOLVED] How do I create a many-to-many Relation?

    I need to bind a DataGridView to a many-to-many Relation.

    Name:  2021-04-25_16-55-42.jpg
Views: 416
Size:  20.7 KB

    I know how to create both of the one-to-many Relations:

    Code:
    'VendorTypes and VendorMembers
    dsMain.Relations.Add("VendorTypes_VendorMembers", dsMain.Tables("VendorTypes").Columns("RecID"), dsMain.Tables("VendorMembers").Columns("VendorTypeID"))
    fkc = dsMain.Relations("VendorTypes_VendorMembers").ChildKeyConstraint
    fkc.DeleteRule = Rule.Cascade
    fkc.UpdateRule = Rule.Cascade
    fkc.AcceptRejectRule = AcceptRejectRule.None
    
    'Vendors and VendorMembers
    dsMain.Relations.Add("Vendors_VendorMembers", dsMain.Tables("Vendors").Columns("RecID"), dsMain.Tables("VendorMembers").Columns("VendorID"))
    fkc = dsMain.Relations("Vendors_VendorMembers").ChildKeyConstraint
    fkc.DeleteRule = Rule.Cascade
    fkc.UpdateRule = Rule.Cascade
    fkc.AcceptRejectRule = AcceptRejectRule.None
    I know how to bind a DataGrid view to a Relation, for example:

    Code:
    With bsFuel
        .DataSource = Nothing
        .DataMember = "Vehicle_Fuel"    '...bind to relationship (before setting the DataSource property, which fires the PositionChanged() event of the BindingSource)
        .DataSource = bsVehicles    '...fires the BindingSource's PositionChanged() event
        .Sort = "VehicleID, Odometer"
    End With
    If flgDebug Then Debug.WriteLine("bsFuel datasource has " & bsFuel.Count & " items.")
    
    dgvFuel.DataSource = bsFuel
    How do I create a many-to-many Relation and use it for binding a DataGridView?

    For example, I have a form that is bound to the VendorTypes DataTable via a BindingSource (bsVendorTypes). When the bsVendorTypes.PositionChanged method is called, I want the Vendors DataGridView (dgvVendors) to automatically show the Vendors that are related to the current VendorType.

    Name:  2021-04-25_18-03-35.jpg
Views: 431
Size:  37.5 KB

    In the above example, I am populating a temporary DataTable (dtTemp) via a temporary DataAdapter (dad) and then binding Vendors DataGridView (dgvVendors) to dtTemp DataTable.

    Code:
    strSQL = "SELECT Vendors.* " &
        "FROM Vendors INNER JOIN VendorMember ON Vendors.RecID = VendorMember.VendorID " &
        "WHERE (VendorMember.VendorTypeID = " & lngVendorID & ") " &
        "ORDER BY Vendors.VendorName, Vendors.VendorCity, Vendors.VendorState"
    
    If con.State <> ConnectionState.Open Then con.Open()
    Using dad As New OleDb.OleDbDataAdapter(strSQL, con)
        dad.Fill(dtTemp)
    End Using
    con.Close()
    
    dgvVendors.DataSource = dtTemp
    
    lblGridInfo.Text = "Vendors (" & dgvVendors.Rows.Count & CStr(IIf(dgvVendors.Rows.Count = 1, " item", " items")) & "):"
    I want to use the many-to-many Relation instead of reading the data from the underlying MS Access database via a DataAdapter.
    Last edited by Mark@SF; Apr 25th, 2021 at 06:05 PM.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width