|
-
Mar 11th, 2010, 01:21 PM
#1
Thread Starter
New Member
Parent/Child/GrandChild
My question is for VB.NET 2008
I have three tables:
Customer (Parent)
AssignedApp (Child)
Application (GrandChild)
Assigned App is simply an associative table used to relate the Customer Table to The Applications table. I am able to set up a Parent/Child relationship between Customer and Assigned App, but not to the grandchild table. Here is the code I have used for the initial Parent/Child, but would actually like to not show the child data, but actually the grandchild data.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Form3
Inherits System.Windows.Forms.Form
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
txtCustomer.Dock = DockStyle.Fill
dgvDetail.Dock = DockStyle.Fill
Dim splitContainer1 As New SplitContainer()
splitContainer1.Dock = DockStyle.Fill
splitContainer1.Orientation = Orientation.Horizontal
splitContainer1.Panel1.Controls.Add(txtCustomer)
splitContainer1.Panel1.Controls.Add(dgvDetail)
Me.Controls.Add(splitContainer1)
Me.Text = "DataGridView master/detail demo"
End Sub
Private dvMaster As New DataView
Private bsMaster As New BindingSource()
Private txtCustomer As New TextBox
Private dgvDetail As New DataGridView()
Private bsDetail As New BindingSource()
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Bind the DataGridView controls to the BindingSource
' components and load the data from the database.
dgvDetail.DataSource = bsDetail
GetData()
txtCustomerName.DataBindings.Add("Text", dvMaster, "CustName")
' Configure the details DataGridView so that its columns automatically
' adjust their widths when the data changes.
dgvDetail.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
End Sub
Private Sub GetData()
Try
Try
' Specify a connection string.
Dim connectionString As String = _
"Integrated Security=SSPI;Persist Security Info=False;" & _
"Initial Catalog=AccessControl;Data Source=localhost"
Dim cn As New SqlConnection(connectionString)
' Create a DataSet.
Dim ds As New DataSet()
' Add data from the Customer table to the DataSet.
Dim masterDataAdapter As New SqlDataAdapter("select * from Customer", cn)
masterDataAdapter.Fill(ds, "Customer")
' Add data from the AssignedApp table to the DataSet.
Dim detailsDataAdapter As New SqlDataAdapter("select * from AssignedApp", cn)
detailsDataAdapter.Fill(ds, "AssignedApp")
' Establish a relationship between Customer and AssignedApp.
Dim relation As New DataRelation("CustomerAssignedApp", _
ds.Tables("Customer").Columns("CustomerID"), _
ds.Tables("AssignedApp").Columns("CustomerID"))
ds.Relations.Add(relation)
' Bind the master data connector to the Customer table.
bsMaster.DataSource = ds
bsMaster.DataMember = "Customer"
dvMaster = New DataView(ds.Tables("Customer"))
' Bind the details data connector to the master data connector,
' using the DataRelation name to filter the information in the
' details table based on the current row in the master table.
bsDetail.DataSource = bsMaster
bsDetail.DataMember = "CustomerAssignedApp"
Catch ex As SqlException
MessageBox.Show("To run this example, replace the value of the " & _
"connectionString variable with a connection string that is " & _
"valid for your system.")
End Try
End Sub
End Class
Last edited by bfullen; Mar 11th, 2010 at 02:33 PM.
Reason: To modify title to reflect .NET version
-
Mar 11th, 2010, 01:31 PM
#2
Re: Parent/Child/GrandChild
I have never heard of the
Parent/Child/GrandChild relationship
I think you mean that the AssignedApp is a Junction table between Customer and Application to resolve a Many to Many relationship
This also appears to be in the worng forum.... You are using .Net and not VB6
Last edited by GaryMazzone; Mar 11th, 2010 at 01:37 PM.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 11th, 2010, 01:44 PM
#3
Thread Starter
New Member
Re: Parent/Child/GrandChild
The actual relationship is as follows:
Customer.CustomerID(pk) (One-to-Many) = AssignedApp.CustomerID(fk)
and
Applications.ApplicationID(pk) (One-to-Many) = AssignedApp.ApplicationID(fk)
Here are the fields if this helps:
Customer
CustomerID(pk)
CustName
LName
FName
etc., etc.
AssignedApp
AssignedID(pk)
CustomerID(fk references Customer.CustomerID)
ApplicationID(fk references Applications.ApplicationID)
Applications
ApplicationID(pk)
ApplicationName
On my form, I would like the Customer table to be bound to text boxes and my datagrid to be bound to the applications the user has access to.
-
Mar 11th, 2010, 01:49 PM
#4
Re: Parent/Child/GrandChild

Since you are using .Net code I've moved this thread to the proper version. You should modify the Subject of the thread to include your .Net version.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|