Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ChildViews and Relations

If a relationship exists between tables in a DataSet, you can create a DataView containing rows from the related child table by using the CreateChildView method of the DataRowView for the rows in the parent table. For example, the following code displays Categories and their related Products in alphabetical order sorted by CategoryName and ProductName.

DataTable catTable = catDS.Tables["Categories"];
DataTable prodTable = catDS.Tables["Products"];

// Create a relation between the Categories and Products tables.
DataRelation relation = catDS.Relations.Add("CatProdRel", 

// Create DataViews for the Categories and Products tables.
DataView catView = new DataView(catTable, "", "CategoryName", 
DataView prodView;

// Iterate through the Categories table.
foreach (DataRowView catDRV in catView)

  // Create a DataView of the child product records.
  prodView = catDRV.CreateChildView(relation);
  prodView.Sort = "ProductName";

  foreach (DataRowView prodDRV in prodView)
    Console.WriteLine("\t" + prodDRV["ProductName"]);
© 2015 Microsoft