VB2019 - DataRelation

  • Thread starter Thread starter Developer Dude
  • Start date Start date
D

Developer Dude

Guest
Hi:

I have a simple program with two tables, SE (Parent Table) and CS (Child Table). I want to display one column (Field) from

the parent table and then have a DataGridView that displays all child columns (Fields) for whatever parent is currently

displayed. I used to know how to do this but it has been years since I used it. Below is my code. I would appreciate some

help. Thanks.

Option Explicit On
Option Strict On

Imports System.Data
Imports System.Reflection
Imports System.Data.OleDb

Public Class Form2_1

'Declarations.
Private bsCS, bsSE As BindingSource

Public Sub New()

'This Call Is Required By The Windows Form Designer.
InitializeComponent()

'Initialize.
bsCS = New BindingSource
bsSE = New BindingSource

End Sub

Private Sub Form2_1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

DataReader_SE()
DataReader_CS()
Data()

End Sub

Private Sub DataReader_SE()

'Create The Parent Table DataReader.
Dim cmdSE As OleDbCommand = New OleDbCommand("SELECT SE_KEY, SE_NAME FROM SE", cnPSB)

Dim drSE As OleDbDataReader

'Load Data Into The Parent DataTable.
cnPSB.Open()
drSE = cmdSE.ExecuteReader()
dsPSB.Load(drSE, LoadOption.OverwriteChanges, "dtSE")
cnPSB.Close()

End Sub

Private Sub DataReader_CS()

'Create The Child DataReader.
Dim cmdCS As OleDbCommand = New OleDbCommand("SELECT CS_SE_KEY, CS_NAME FROM CS", cnPSB)

Dim drCS As OleDbDataReader

'Load Data Into The Child DataTable.
cnPSB.Open()
drCS = cmdCS.ExecuteReader()
dsPSB.Load(drCS, LoadOption.OverwriteChanges, "dtCS")
cnPSB.Close()

End Sub

Private Sub BtnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click

bsSE.MoveFirst()

End Sub

Private Sub BtnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click

bsSE.MovePrevious()

End Sub

Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click

bsSE.MoveNext()

End Sub

Private Sub BtnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click

bsSE.MoveLast()

End Sub

Private Sub Data()
'Create A DataRelation.
Dim data_relation As New _
DataRelation("SE_CS",
dsPSB.Tables("dtSE").Columns("SE_KEY"), _
_
dsPSB.Tables("dtCS").Columns("CS_SE_KEY"))
dsPSB.Relations.Add(data_relation)


'Parent Table BindingSource.
bsSE.DataSource = dsPSB
bsSE.DataMember = "dtSE"

Label1.DataBindings.Add("Text", bsSE, "SE_NAME")

'Child Table BindingSource.
bsCS.DataSource = dsPSB
bsCS.DataMember = "SE_CS"

DataGridView2.DataSource = bsCS

End Sub

End Class

Continue reading...
 

Similar threads

D
Replies
0
Views
131
Developer Dude
D
D
Replies
0
Views
106
Developer Dude
D
D
Replies
0
Views
139
Developer Dude
D
Back
Top