Navigating Hierarchical Data

The strongly typed DataSet provides two methods for each DataRelation , TableName Row( ) and TableName Rows( ), to facilitate navigating records in parent child relationships. These methods are similar to the GetParentRow( ) and GetChildRows( ) methods in the untyped DataSet. The TableName Row( ) method is defined for the child table and retrieves the parent row for a DataRow. The TableName Rows( ) method is defined for the parent table in a relationship and retrieves the child rows for a DataRow.

The strongly typed DataSet methods encapsulate the DataRelations defined within the DataSet so a reference to the DataRelation or the name of the DataRelation isn’t required when navigating the hierarchy of records. The following sample demonstrates using the strongly typed DataSet methods to navigate a hierarchy of records:

// strongly typed DataSet called Northwind containing Orders table and // OrderDetails table, related through the OrderID field Northwind ds = new Northwind(); // ... code to fill the Orders and Order Details tables in the DataSet foreach(Northwind.OrdersRow ordersRow in ds.Orders) { // iterate the collection of order details for the order through // the GetOrderDetailsRow accessor foreach(Northwind.Order_DetailsRow orderDetailsRow in ordersRow.GetOrder_DetailsRows()) { // get the CustomerID from the parent row, through the // OrdersRow property of the child row String customerId = orderDetailsRow.OrdersRow.CustomerID; // get the ProductID ...

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.