1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| using System;
using System.Data;
namespace BindingTreeViewToDataSet
{
public static class DataSetCreator
{
public static DataSet CreateDataSet()
{
DataSet ds = new DataSet();
// Create the parent table.
// ***************************************
DataTable tbl = new DataTable( "Master" );
tbl.Columns.Add( "ID", typeof( int ) );
tbl.Columns.Add( "Name" );
for( int i = 0; i < 5; ++i )
{
DataRow row = tbl.NewRow();
row["ID"] = i;
row["Name"] = "Master #" + i;
tbl.Rows.Add( row );
}
ds.Tables.Add( tbl );
// Create the child table.
// ***************************************
tbl = new DataTable( "Detail" );
tbl.Columns.Add( "MasterID", typeof( int ) );
tbl.Columns.Add( "Info" );
for( int i = 0; i < 16; ++i )
{
DataRow row = tbl.NewRow();
row["MasterID"] = i % 5;
row["Info"] = String.Format(
"Detail Info #{0} for Master #{1}",
(i / 5), (i % 5) );
tbl.Rows.Add( row );
}
ds.Tables.Add( tbl );
// Associate the tables.
// ***************************************
ds.Relations.Add(
"Master2Detail",
ds.Tables["Master"].Columns["ID"],
ds.Tables["Detail"].Columns["MasterID"] );
return ds;
}
}
} |