Return Child Records

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hello:

I'm working on an app that has many child tables. Like 26 child tables in a
MS Access Database.

I created the form, adapters, DS and relations in designer.

I also added the parameters in designer to find records when a user types
into a textbox.

My problem is I can't figure out how to load the child records onto the
form.

I don't want to use a datagride because there will be only one child record
for each parent record. The client dosn't want a grid either.

Also, there are about 325 fields so I can't just put them all in one table
as Access only accepts 255 fields per table and I don't want to have to hand
code all the insert, update and load statements for every single controle on
the form. I will use a groupbox visible property as the user switches from
one table to the next.

I guess what I'm asking is if there is a way to load the child rows using
the relationship that I also setup in designer.

Thanks

Bob
 
Hello Bob

There is method for datarow
DataRow.GetChildRows

I think this will solve your problem.
Please refer following code.
--------------------------------------------------------------------
[Visual Basic]
Private Sub GetChildRowsFromDataRelation(myTable As DataTable)
Dim myRelation As DataRelation
Dim arrRows() As DataRow
Dim myRow As DataRow
Dim i As Integer
Dim myColumn As DataColumn

For Each myRelation In myTable.ChildRelations
For Each myRow In myTable.Rows
arrRows = myRow.GetChildRows(myRelation,
DataRowVersion.Proposed)
' Print values of rows.
For i = 0 To arrRows.GetUpperBound(0)
For Each myColumn in myTable.Columns
Console.WriteLine(arrRows(i)(myColumn))
Next myColumn
Next i
Next myRow
Next myRelation
End Sub

[C#]
private void GetChildRowsFromDataRelation(DataTable myTable ) {
DataRow[] arrRows;
foreach(DataRelation myRelation in myTable.ChildRelations){
foreach(DataRow myRow in myTable.Rows){
arrRows = myRow.GetChildRows(myRelation,
DataRowVersion.Proposed);
// Print values of rows.
for(int i = 0; i < arrRows.Length; i++){
foreach(DataColumn myColumn in myTable.Columns){
Console.WriteLine(arrRows[myColumn]);
}
}
}
}
}
 
bhawin13:

Thanks for the respnse. It's been a few days because I've been kind of
busy.

Thats the track I'm on but the problem I'm having is that I'm trying to do
this without typing for all my controles because I have many fields in my
tables.

I'm passing a param to the child tables. All are untyped datasets and
params.

I was hoping I could filter the child table with the value recieved from

For Each myColumn in myTable.Columns
Console.WriteLine(arrRows(i)(myColumn))
Next myColumn

Such as:

Dim drPage01 As DataRow
For Each drPage01 In draPage01
ClientIDVal = (drPage01("ClientID"))
Next

and passing the variable(Yes it's declared at the top of the form)
ClientIDVal to the param for the child table

The problem is a sort of visious circle.

VB insites I fill both the parent and child dataadapters before I can get
the value from GetChildRowsFromDataRelation(myTable As DataTable)

When I try ti fill the child adapter, of course it wants a value for the
param.

It seems that VB relationships don't work like Access relations.

I'll put the code below if you feel like looking at it. Keep in mind I have
327 fields within 26 tables and I don't realy want to have to hand code
every controle. ie mytextbox.Text = (drPage01("ClientName")) etc for each
and every field.

My code:

Private Sub txtFindOffFileNum_KeyPress1(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.KeyPressEventArgs) Handles
txtFindOffFileNum.KeyPress

If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then

' Enter key pressed! - do what you like here

'objdsClients.Clear()

Try
'Attempt to load the dataset.
'Me.LoadDataSet()
daClientsMain.SelectCommand.Parameters("ClientID").Value =
txtFindClID.Text

daClientsMain.SelectCommand.Parameters("OfficeFileNum").Value = 0
Me.daClientsMain.Fill(objdsClients)

Me.daPage01.Fill(objdsClients) ****Produces an error
because it's expecting a parameter not yet retrieved by code below********

Dim CurrentClientID As String
CurrentClientID = Me.editClientID.Text
Dim drCurrentClient As DataRow
drCurrentClient =
objdsClients.ClientsMain.FindByClientID(CurrentClientID)
Dim draPage01 As DataRow()
draPage01 = drCurrentClient.GetChildRows("PK_ClientID")
Me.Text = draPage01.Length.ToString() & " Client Pages " &
CurrentClientID

'ClientIDVal = editClientID.Text()
Dim drPage01 As DataRow
For Each drPage01 In draPage01
ClientIDVal = (drPage01("ClientID"))
Next

'daPage01.SelectCommand.Parameters("ClientID").Value =
ClientIDVal
'Me.daPage01.Fill(objdsClients) ********Does nothing
because the DA has to be filled before the above code for a value to be
present*************

***Seems both dataadapters have to be filled before you can get
relational information wich makes NO sense. If thats the case, then what
good are relationships in VB ? ?*******************

Catch eLoad As System.Exception
'Add your error handling code here.
'Display error message, if any.
System.Windows.Forms.MessageBox.Show(eLoad.Message)
End Try
Me.objdsClients_PositionChanged()
e.Handled = True
End If
End Sub

Hope you can help me out with this.

Bob
 
Back
Top