With help, we can find the following: Code Implementation
Example
[C #] the following example creates two able objects and a datarelation object, and adds these new objects to dataset. Call the DataGrid. setdatabinding method to display these tables in the DataGrid Control.
[C #]
// Put the next line into the declarations section.
Private system. Data. dataset mydataset;
Private void makedatatables (){
// Run all of the functions.
Makeparenttable ();
Makechildtable ();
Makedatarelation ();
Bindtodatagrid ();
}
Private void makeparenttable (){
// Create a new datatable.
System. Data. datatable mydatatable = new datatable ("parenttable ");
// Declare variables for datacolumn and datarow objects.
Datacolumn mydatacolumn;
Datarow mydatarow;
// Create new datacolumn, set datatype, columnname and add to datatable.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. int32 ");
Mydatacolumn. columnname = "ID ";
Mydatacolumn. readonly = true;
Mydatacolumn. Unique = true;
// Add the column to the datacolumncollection.
Mydatatable. Columns. Add (mydatacolumn );
// Create second column.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. String ");
Mydatacolumn. columnname = "parentitem ";
Mydatacolumn. autoincrement = false;
Mydatacolumn. Caption = "parentitem ";
Mydatacolumn. readonly = false;
Mydatacolumn. Unique = false;
// Add the column to the table.
Mydatatable. Columns. Add (mydatacolumn );
// Make the ID column the primary key column.
Datacolumn [] primarykeycolumns = new datacolumn [1];
Primarykeycolumns [0] = mydatatable. Columns ["ID"];
Mydatatable. primarykey = primarykeycolumns;
// Instantiate the dataset variable.
Mydataset = new dataset ();
// Add the new datatable to the dataset.
Mydataset. Tables. Add (mydatatable );
// Create three new datarow objects and add them to the datatable
For (INT I = 0; I <= 2; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["ID"] = I;
Mydatarow ["parentitem"] = "parentitem" + I;
Mydatatable. Rows. Add (mydatarow );
}
}
Private void makechildtable (){
// Create a new datatable.
Datatable mydatatable = new datatable ("childtable ");
Datacolumn mydatacolumn;
Datarow mydatarow;
// Create first column and add to the datatable.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. int32 ");
Mydatacolumn. columnname = "childid ";
Mydatacolumn. autoincrement = true;
Mydatacolumn. Caption = "ID ";
Mydatacolumn. readonly = true;
Mydatacolumn. Unique = true;
// Add the column to the datacolumncollection.
Mydatatable. Columns. Add (mydatacolumn );
// Create second column.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. String ");
Mydatacolumn. columnname = "childitem ";
Mydatacolumn. autoincrement = false;
Mydatacolumn. Caption = "childitem ";
Mydatacolumn. readonly = false;
Mydatacolumn. Unique = false;
Mydatatable. Columns. Add (mydatacolumn );
// Create third column.
Mydatacolumn = new datacolumn ();
Mydatacolumn. datatype = system. type. GetType ("system. int32 ");
Mydatacolumn. columnname = "parentid ";
Mydatacolumn. autoincrement = false;
Mydatacolumn. Caption = "parentid ";
Mydatacolumn. readonly = false;
Mydatacolumn. Unique = false;
Mydatatable. Columns. Add (mydatacolumn );
Mydataset. Tables. Add (mydatatable );
// Create three sets of datarow objects, five rows each, and add to datatable.
For (INT I = 0; I <= 4; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["childid"] = I;
Mydatarow ["childitem"] = "item" + I;
Mydatarow ["parentid"] = 0;
Mydatatable. Rows. Add (mydatarow );
}
For (INT I = 0; I <= 4; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["childid"] = I + 5;
Mydatarow ["childitem"] = "item" + I;
Mydatarow ["parentid"] = 1;
Mydatatable. Rows. Add (mydatarow );
}
For (INT I = 0; I <= 4; I ++ ){
Mydatarow = mydatatable. newrow ();
Mydatarow ["childid"] = I + 10;
Mydatarow ["childitem"] = "item" + I;
Mydatarow ["parentid"] = 2;
Mydatatable. Rows. Add (mydatarow );
}
}
Private void makedatarelation (){
// Datarelation requires two datacolumn (Parent and Child) and a name.
Datarelation mydatarelation;
Datacolumn parentcolumn;
Datacolumn childcolumn;
Parentcolumn = mydataset. Tables ["parenttable"]. Columns ["ID"];
Childcolumn = mydataset. Tables ["childtable"]. Columns ["parentid"];
Mydatarelation = new datarelation ("parent2child", parentcolumn, childcolumn );
Mydataset. Tables ["childtable"]. parentrelations. Add (mydatarelation );
}
Private void bindtodatagrid (){
// Instruct the DataGrid to bind to the dataset, with
// Parenttable as the topmost datatable.
Datagrid1.setdatabinding (mydataset, "parenttable ");
}
However, in the actual development process, we generally read data through the database and put it in dataset. The following shows how to connect to the data.
Private dataset myds;
Myds = new dataset ();
Sqldatabase sqldb = new sqldatabase (DSN );
String strsql = "select * from ";
System. Data. dataset DS = sqldb. createset (strsql, "");
Datatable DT;
Dt = Ds. Tables [0]. Clone ();
Myds. Tables. Add (DT );
String strsql1 = "select * from B ";
System. Data. dataset ds1 = sqldb. createset (strsql1, "B ");
Datatable dt1;
Dt1 = ds1.tables [0]. Clone ();
Myds. Tables. Add (dt1 );
Sqldb. closeconnection ();
Myds. Merge (Ds. Tables [0]);
Myds. Merge (ds1.tables [0]);
This. datagrid1.datasource = myds;
This. datagrid1.databind ();
This. datagrid2.datasource = myds. Tables [1];
This. Maid ();