Powered By Blogger

Search

Monday, April 7, 2008

Relation objects in a dataset

How to use relation objects in a dataset?
DataSet's that contain multiple DataTable objects can use DataRelation objects to relate one table to another. Adding a DataRelation to a DataSet adds by default a UniqueConstraint to the parent table and a ForeignKeyConstraint to the child table.
The code sample below creates a DataRelation using two DataTable objects in a DataSet. Each DataTable contains a column named CustID which serves as a "relation" between two the DataTable objects. The example adds a single DataRelation to the Relations collection of the DataSet. The first argument in the sample specifies the name of the DataRelation being created. The second argument sets the parent DataColumn and the third argument sets the child DataColumn.

custDS.Relations.Add("CustOrders",custDS.Tables["Customers"].Columns["CustID"],custDS.Tables["Orders"].Columns["CustID"]);
OR

private void CreateRelation()
{
DataColumn parentCol;DataColumn childCol;
parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
childCol = DataSet1.Tables["Orders"].Columns["CustID"];
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
DataSet1.Relations.Add(relCustOrder);

No comments: