ChildViews and Relations (ADO.NET)
.NET Framework 4
If a relationship exists between tables in a DataSet1, you can create a DataView2 containing rows from the related child table by using the CreateChildView3 method of the DataRowView4 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", catTable.Columns["CategoryID"], prodTable.Columns["CategoryID"]); // Create DataViews for the Categories and Products tables. DataView catView = new DataView(catTable, "", "CategoryName", DataViewRowState.CurrentRows); DataView prodView; // Iterate through the Categories table. foreach (DataRowView catDRV in catView) { Console.WriteLine(catDRV["CategoryName"]); // 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"]); }
Links Table
1http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx
2http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx
3http://msdn.microsoft.com/en-us/library/system.data.datarowview.createchildview.aspx
4http://msdn.microsoft.com/en-us/library/system.data.datarowview.aspx
5http://msdn.microsoft.com/en-us/library/fdcwwhez.aspx
6http://go.microsoft.com/fwlink/?LinkId=217917
