I need to establish a relationship that looks like this:
One Payment can have many PaymentLine
One PaymentLine can have one Payer
One PaymentLine can have many PaymentLine Details
The relationships that I create between Payment and
PaymentLine work, and the one between PaymentLine and
PaymentLine detail work. Whenever, I bind the dataset to
a grid, it will never pick up the auto-identity for a
payment and payer on a payment line. How can I make this
work? Any help or insight would greatly be appreciated.
Here is some of the code that I built into a winform
testing application:
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As
System.Windows.Forms.Button
Friend WithEvents DataGrid1 As
System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point
(16, 16)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point
(24, 56)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(680,
240)
Me.DataGrid1.TabIndex = 1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(712, 318)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim myPmtTable As New DataTable("Pmt")
Dim myColumn As New DataColumn
Dim ds As New DataSet
myPmtTable.Columns.Add(AddColumn
("Pmt_id", "System.Int32", True))
'Add table to dataset
ds.Tables.Add(myPmtTable)
Dim myPmtLnTable As New DataTable("PmtLn")
myPmtLnTable.Columns.Add(AddColumn
("PmtLn_id", "System.Int32", True))
myPmtLnTable.Columns.Add(AddColumn
("Pmt_id", "System.Int32", False))
myPmtLnTable.Columns.Add(AddColumn
("Payer_id", "System.Int32", False))
'Add table to dataset
ds.Tables.Add(myPmtLnTable)
Dim myPmtLnDetTable As New DataTable("PmtLnDet")
myPmtLnDetTable.Columns.Add(AddColumn
("PmtLnDet_id", "System.Int32", True))
myPmtLnDetTable.Columns.Add(AddColumn
("PmtLn_id", "System.Int32", False))
myPmtLnDetTable.Columns.Add(AddColumn
("PmtLnDet_amount", "System.Double", False))
'Add table to dataset
ds.Tables.Add(myPmtLnDetTable)
Dim myPayerTable As New DataTable("Payer")
myPayerTable.Columns.Add(AddColumn
("Payer_id", "System.Int32", True))
myPayerTable.Columns.Add(AddColumn
("Payer_name", "System.String", False))
ds.Tables.Add(myPayerTable)
'Create another dataset, just to see what happens
Dim myDatasetWithRelations As DataSet =
BuildPaymentDataRelations(ds)
DataGrid1.DataSource = myDatasetWithRelations
End Sub
Public Function AddColumn(ByVal ColumnName As String,
ByVal DataType As String, ByVal autoIncrement As Boolean)
As DataColumn
Dim myColumn As New DataColumn
myColumn.ColumnName = ColumnName
myColumn.DataType = Type.GetType(DataType)
myColumn.AutoIncrement = autoIncrement
If autoIncrement Then
myColumn.AutoIncrementSeed = -1
End If
Return myColumn
End Function
Private Function BuildPaymentDataRelations(ByVal
dataset As DataSet) As DataSet
'blnRelationsCreated = True
Dim parentCol, childCol As DataColumn
parentCol = dataset.Tables("Pmt").Columns
("pmt_id")
childCol = dataset.Tables("PmtLn").Columns
("pmt_id")
dataset.Relations.Add("Pmt_PmtLn", parentCol,
childCol)
parentCol = dataset.Tables("Payer").Columns
("payer_id")
childCol = dataset.Tables("PmtLn").Columns
("payer_id")
dataset.Relations.Add("Payer_PmtLn", parentCol,
childCol)
parentCol = dataset.Tables("PmtLn").Columns
("pmtln_id")
childCol = dataset.Tables("PmtLnDet").Columns
("pmtln_id")
dataset.Relations.Add("PmtLn_PmtLnDet",
parentCol, childCol)
Return dataset
End Function
End Class