System.Data Namespace DataSet Class
Returns the collection of relations that link tables and allow navigation from parent tables to child tables.
Script |
[ DataRelationCollection variable = ] DataSet.Relations |
A DataRelationCollection that contains a collection of DataRelation objects; otherwise a null value if no DataRelation objects exist.
The property is read only with no default value.
The following examples demonstrate using the Relations property to access the parent/child relationship between tables in a DataSet.
The first example uses generic code to loop thru the parent and child rows of a given relationship, using the DataRow.GetChildRows ( DataRelation ) method.
// iterate over parent rows
foreach ( DataRow parentRow in myTable.Rows ) {
// get parent row
Response.Write ( parentRow [ "parentFieldName" ] );
// iterate over child rows
foreach ( DataRow childRow in parentRow.GetChildRows ( "relationName" ) ) {
// get child row
Response.Write ( childRow [ "childFieldName" ] );
}
}
' iterate over parent rows
Dim parentRow As DataRow
For Each parentRow In myTable.Rows
' get parent row
Response.Write ( parentRow ( "parentFieldName" ) )
' iterate over child rows
Dim childRow As DataRow
For Each childRow In parentRow.GetChildRows ( "relationName" )
' get child row
Response.Write ( childRow ( "childFieldName" ) )
Next childRow
Next parentRow |
|
C# |
VB |
The second example is essentially the same, except that it uses the DataRowView.CreateChildView ( DataRelation ) method, and renders the parent and child rows using nested templated controls. The below code shows the method to bind the child control's data source.
void getChildSource ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
DataGrid childGrid = ( DataGrid ) e.Item.FindControl ( "Products" );
childGrid.DataSource = ( ( DataRowView ) e.Item.DataItem ).CreateChildView ( "categoryId" );
childGrid.DataBind ( );
}
}
Sub getChildSource ( src As Object, e As DataListItemEventArgs )
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim childGrid As DataGrid = CType ( e.Item.FindControl ( "Products" ), DataGrid )
childGrid.DataSource = CType ( e.Item.DataItem, DataRowView ).CreateChildView ( "categoryId" )
childGrid.DataBind ( )
End If
End Sub |
|
C# |
VB |
DataSet Members DataRelation DataTable DataRelationCollection