通过帮助,我们可以查出如下代码实现
示例
[ C#] 以下示例创建两个 DataTable 对象和一个 DataRelation 对象,并将这些新对象添加到 DataSet 中。然后通过调用 DataGrid.SetDataBinding 方法在 DataGrid 控件中显示这些表。
[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 the
// ParentTable as the topmost DataTable.
dataGrid1.SetDataBinding(myDataSet,"ParentTable");
}
但是在实际的开发过程中,我们一般是通过数据库读取数据,然后放到DataSet中,下面我使用演示如何通过连接数据实现
private DataSet myds;
myds=new DataSet();
SqlDataBase SqlDB=new SqlDataBase(DSN);
string strsql=" select * from A ";
System.Data.DataSet ds=SqlDB.CreateSet(strsql,"A");
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.DataGrid2.DataBind();