asp.net.ph

DataSet.Relations Property

System.Data Namespace   DataSet Class


Returns the collection of relations that link tables and allow navigation from parent tables to child tables.

Syntax


Script [ DataRelationCollection variable = ] DataSet.Relations

Return Value

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.

Example

The following examples demonstrate using the Relations property to access the parent/child relationship between tables in a DataSet.

DataRow GetChildRows Method
Run Sample | View Source
DataRowView CreateChildView Method
Run Sample | View Source

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" ] );
   }
}
  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 ( );
   }
}
  C# VB

See Also

DataSet Members   DataRelation   DataTable   DataRelationCollection Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph