Creating ChildDataViewObjects
You can create a childDataView from a parent DataRowView using the
CreateChildView() method. You can then view the DataRowView objects from the child
DataView. To call the CreateChildView() method, you must first add a DataRelation to
the DataSet that defines a relationship between the two underlying DataTable objects.
(See Chapter 12
, "Navigating and Modifying Related Data," for information about
DataRelation objects.)
Let's take a look at an example. Assume you have two DataTable objects named
customersDT and ordersDT. Also assume you've added the following DataRelation to the
DataSet that defines a relationship between customersDT and ordersDT:
DataRelation customersOrdersDataRel =
new DataRelation(
"CustomersOrders",
customersDT.Columns["CustomerID"],
ordersDT.Columns["CustomerID"]
);
myDataSet.Relations.Add(
customersOrdersDataRel
);
Finally, assume you have a DataView named customersDV that views the customers that
have a Country column of UK. You can then call the CreateChildView() method from a
DataRowView in customersDV to create a child DataView; notice that the name of the
DataRelation (CustomersOrders) is passed to the CreateChildView() method:
DataView ordersDV = customersDV[0].CreateChildView("CustomersOrders");
The ordersDV DataView allows you to access the child rows from the ordersDT
DataTable. The parent in this example is the first DataRowView from customersDV with
a CustomerID of AROUT. The child ordersDV DataView contains DataRowView
objects with the details of the orders for the AROUT customer.
N
ote The CreateChildView() method is overloaded. The other version of this method
accepts a DataRelation object as the parameter.
Listing 13.4
shows a complete example program.
Listing 13.4: CREATECHILDDATAVIEW.CS
/*
CreateChildDataView.cs illustrates how to create a
childDataView
*/
using System;
using System.Data;
using System.Data.SqlClient;
class CreateChildDataView
{
public static void Main()
{
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName, Country " +
"FROM Customers;" +
"SELECT OrderID, CustomerID " +
"FROM Orders;";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
mySqlDataAdapter.Fill(myDataSet);
mySqlConnection.Close();
myDataSet.Tables["Table"].TableName = "Customers";
myDataSet.Tables["Table1"].TableName = "Orders";
DataTable customersDT = myDataSet.Tables["Customers"];
DataTable ordersDT = myDataSet.Tables["Orders"];
// add a DataRelation object to myDataSet
DataRelation customersOrdersDataRel =
new DataRelation(
"CustomersOrders",
customersDT.Columns["CustomerID"],
ordersDT.Columns["CustomerID"]
);
myDataSet.Relations.Add(
customersOrdersDataRel
);
// create a DataView object named customersDV
DataView customersDV = new DataView();
customersDV.Table = customersDT;
customersDV.RowFilter = "Country = 'UK'";
customersDV.Sort = "CustomerID";
// display the first row in the customersDV DataView object
Console.WriteLine("Customer:");
for (int count = 0; count < customersDV.Table.Columns.Count; count++)
{
Console.WriteLine(customersDV[0][count]);
}
// create a childDataView named ordersDV that views
// the child rows for the first customer in customersDV
DataView ordersDV = customersDV[0].CreateChildView("CustomersOrders");
// display the child rows in the customersDV DataView object
Console.WriteLine("\nOrderID's of the orders placed by this customer:");
foreach (DataRowView ordersDRV in ordersDV)
{
Console.WriteLine(ordersDRV["OrderID"]);
}
}
}
The output from this program is as follows:
Customer:
AROUT
Around the Horn
UK
OrderID's of the orders placed by this customer:
10355
10383
10453
10558
10707
10741
10743
10768
10793
10864
10920
10953
11016
.
Creating Child DataView Objects
You can create a child DataView from a parent DataRowView using the
CreateChildView() method. You. example program.
Listing 13.4: CREATECHILDDATAVIEW.CS
/*
CreateChildDataView.cs illustrates how to create a
child DataView
*/
using System;
using