Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a child datarow to an existing datarow array

Status
Not open for further replies.

NatHunter

Technical User
Aug 17, 2001
51
0
0
GB
Hi Folks,

I want to add a child datarow to an existing datarow array - is this possible?

Basically I am looping through one datarow, using getchildrows to get each child and I want to return a datarow array of all the children.

It is the line 'rowChildren(shChildCnt) = rowChild' that I am struggling with. Thanks for any help on this!

shHasChildCount = 0
shChildCnt = 0
For Each rowParent In rowParents
For Each rowChild In rowParent.GetChildRows(sRelation)
If rowChild.RowState <> DataRowState.Deleted Then
'add the child row to the children rows
rowChildren(shChildCnt) = rowChild
shChildCnt = shChildCnt + 1
End If
Next rowChild
If rowChildren.Length > 0 Then
shHasChildCount = shHasChildCount + 1
End If
Next

Paul
 
I dont think you can add to a datarow array. All you can do is iterate through them. However you could always use a collection to hold your datarows as follows

Code:
'Add rows to Collection
Dim prows As DataRow() = dt.Select
Dim c As New Collection
For Each r As DataRow In prows
    c.Add(r)
Next

'Scan through Collection
For i As Integer = 0 To c.Count - 1
    Dim r As DataRow = CType(c.Item(i), DataRow)
Next


Sweep
...if it works dont mess with it
 
I would think you could redim preserve the array then cycle through the columns to get the data:

Code:
public sub AddDR(NewDR as datarow)
'redim the array 1 longer
redim preserve dr(dr.length)

  'loop through all of the cols from the table and 
  'assign the values from the new row
  Dim iCol As Integer
  For iCol = 0 To dr(dr.ubound(0)).Table.Columns.Count - 1
    dr(dr.ubound(0)).Item(iCol) = NewDr.item(iCold)
  Next
end sub

This assumes that your new row is identical in structure to your old row. It may be easier though to just use a data table.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top