Oggetti ChildView e relazioniChildViews and Relations

Se esiste una relazione tra le tabelle di un tipo DataSet, è possibile creare un tipo DataView contenente le righe della tabella figlio correlata usando il metodo CreateChildView di DataRowView per le righe della tabella padre.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. Ad esempio, il codice seguente consente di visualizzare categorie e le relative prodotti in ordine alfabetico per CategoryName e ProductName .For example, the following code displays Categories and their related Products in alphabetical order sorted by CategoryName and ProductName.

Dim catTable As DataTable = catDS.Tables("Categories")  
Dim prodTable As DataTable = catDS.Tables("Products")  

' Create a relation between the Categories and Products tables.  
Dim relation As DataRelation = catDS.Relations.Add("CatProdRel", _  
  catTable.Columns("CategoryID"), _  
  prodTable.Columns("CategoryID"))  

' Create DataViews for the Categories and Products tables.  
Dim catView As DataView = New DataView(catTable, "", _  
  "CategoryName", DataViewRowState.CurrentRows)  
Dim prodView As DataView  

' Iterate through the Categories table.  
Dim catDRV, prodDRV As DataRowView  

For Each catDRV In catView  
  Console.WriteLine(catDRV("CategoryName"))  

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

  For Each prodDRV In prodView  
    Console.WriteLine(vbTab & prodDRV("ProductName"))  
  Next  
Next  
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"]);  
}  

Vedere ancheSee Also

DataSet
DataView
DataRowView
DataViewDataViews
Provider gestiti ADO.NET e Centro per sviluppatori di set di datiADO.NET Managed Providers and DataSet Developer Center