Q: An array of rows

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Suppose we have an array of DataRows e.g.

Dim drMyRows() As DataRow = myTable.Rows(0).GetChildRows("PricesCompany")

generated via a relationship.

The question I have is this. Isn't this in essence a table i.e. a collection
of rows? The reason I ask is that I wanted to extract from the array of rows
certain rows satisfying a given condition. Now, if this had been a
DataTable, I could have done something like:

Dim strCriteria As String = "Prices = '2' AND Company = 'ABC Ltd'"
Dim strSortOrder As String = "Town DESC"

Dim aRows As DataRow() myNewTable.Select(strCriteria, strSortOrder)

Is there a way of doing this with the array of rows? Ok, I suppose I could
create a new table using the array of rows but this will surely increase the
amount of processing. Is there an easier and more efficient way to do this?

Thanks in advance

Geoff
 
Hi Geoff,

When you have the datarow(0) you know probably as well the criteria to use
in your related table for a dataview. Why do you not use that, looks for me
a lot simpler.

(I was first busy to tell you that when you go this way you can make a
datatable by cloning the related table and than use the importrow in a for
each loop, however thinking about that, it did look for me the wrong way)

Cor
 
Geoff,
The question I have is this. Isn't this in essence a table i.e. a collection
of rows?
Yes its a collection of Rows, however a Table is significantly more then a
collection of Rows, as evidenced by the Methods, Properties, and Events
unique to a DataTable, that System.Array does not have.
Is there a way of doing this with the array of rows?
No! As you are finding you, an Array has no special powers. System.Array has
a couple shared methods that may help, however if I were using the DataSet
object model, I would attempt to stay with the DataSet OM.
Ok, I suppose I could
create a new table using the array of rows but this will surely increase the
amount of processing. Is there an easier and more efficient way to do
this?
I would consider leaving them in the original DataTable, before I decided
copying them into a new DataTable, as copying them to a new DataTable will
do just that, create a copy of the rows. If you leave them in the Array or
DataView, then the rows themselves are still part of the original DataTable,
the array & DataView only have references to these original rows. Read a new
DataTable introduces aliasing problems, which you may be able to solve with
DataSet.Merge.


Depending on what I needed to do with the "extract" of the ChildRows, I
would probably simply use a For Each on the ChildRows, checking each for the
specific criteria.
Dim drMyRows() As DataRow = myTable.Rows(0).GetChildRows("PricesCompany")
For Each row As DataRow in drMyRows
If row!Prices = 2 Then
' do something exciting with this row.
End If
Next

Hope this helps
Jay
 
Many thanks Jay for your help.

Geoff

Jay B. Harlow said:
Geoff,
Yes its a collection of Rows, however a Table is significantly more then a
collection of Rows, as evidenced by the Methods, Properties, and Events
unique to a DataTable, that System.Array does not have.

No! As you are finding you, an Array has no special powers. System.Array has
a couple shared methods that may help, however if I were using the DataSet
object model, I would attempt to stay with the DataSet OM.

this?
I would consider leaving them in the original DataTable, before I decided
copying them into a new DataTable, as copying them to a new DataTable will
do just that, create a copy of the rows. If you leave them in the Array or
DataView, then the rows themselves are still part of the original DataTable,
the array & DataView only have references to these original rows. Read a new
DataTable introduces aliasing problems, which you may be able to solve with
DataSet.Merge.


Depending on what I needed to do with the "extract" of the ChildRows, I
would probably simply use a For Each on the ChildRows, checking each for the
specific criteria.
myTable.Rows(0).GetChildRows("PricesCompany")
For Each row As DataRow in drMyRows
If row!Prices = 2 Then
' do something exciting with this row.
End If
Next

Hope this helps
Jay
 
Geoff,
Rereading Cor's post, I believe DataRowView.CreateDataView will actually do
what you want...

Something like:

Dim view As New DataView(myTable)

For Each parent As DataRowView In view
Dim prices As DataView = parent.CreateChildView("PricesCompany")
prices.RowFilter = "Prices = '2'"
For Each price As DataRowView In prices
Debug.WriteLine(price!Prices, "Prices")
Debug.WriteLine(price!Company, "Company")
Next
Next

I tested the following in VS.NET 2003, with data from the SQL Server
Northwind sample database:

Dim view As New DataView(customerDataSet.Tables("Customers"))
For Each customer As DataRowView In view
Dim orders As DataView =
customer.CreateChildView("CustomerOrders")
orders.RowFilter = "OrderDate = #2/26/1998#"
For Each order As DataRowView In orders
Debug.WriteLine(order!CustomerID, "CustomerID")
Debug.WriteLine(order!OrderId, "Order")
Next
Next

The customer.CreateChildView("CustomerOrders") returns a special DataView
that only has the rows of the child rows available, the RowFilter is in
addition to the relationship...

Hope this helps
Jay
 
Back
Top