Infragistics WinGrid如何通过代码绑定多层级数据源
该实例我们简单介绍WinGrid如何绑定两个DataTables,并设置两个数据表之间的相互关系,Infragistics WinGrid是Infragistics Professional产品里Windows Form平台下的一个表格控件,该产品几乎可以实现数据表格展示的所有功能,数据绑定,增加,删除,更新,排序,分组等,并且产品提供了各种事例和帮助文档,帮助开发人员在最短时间内上手,这里我们简单介绍下该产品如果绑定使用代码产生的数据。
主要步骤如下:
1.首先使用代码创建2个数据表DataTable
2. 把创建好的两个数据表添加到DataSet,并创建两个DataTable之间的数据关系
3. 绑定多层DataTable到UltraGrid
该实例的运行效果图如下:
具体代码如下:
1. 首先我们创建一个新的桌面应用程序,直接从IDE的工具箱里拖拽UltraGrid和UltraButton控件到窗体上,并创建第一个DataTable:
private DataTable MakeCustomersDataTable()
{
// Declare a DataTable to contain the program generated data
DataTable dataTable = new DataTable("Customers");
// Create and add a CustomerID column
DataColumn colWork = new DataColumn("CustomerID", System.Type.GetType("System.Int32"));
dataTable.Columns.Add(colWork);
// Add CustomerID column to key array and bind to DataTable
DataColumn[] Keys = new DataColumn[1];
Keys[0] = colWork;
dataTable.PrimaryKey = Keys;
// Create and add a CustomerName column
colWork = new DataColumn("CustomerName", System.Type.GetType("System.String"));
colWork.MaxLength = 50;
dataTable.Columns.Add(colWork);
// Create and add a L=tOrderDate column
colWork = new DataColumn("L=tOrderDate", System.Type.GetType("System.DateTime"));
dataTable.Columns.Add(colWork);
// Add a row
DataRow row = dataTable.NewRow();
row["CustomerID"] = 1;
row["CustomerName"] = "Johns Widgets";
row["L=tOrderDate"] = System.DateTime.Now;
dataTable.Rows.Add(row);
// Add another row
row = dataTable.NewRow();
row["CustomerID"] = 2;
row["CustomerName"] = "Freds Thingamagigs";
row["L=tOrderDate"] = System.DateTime.Now.AddDays(-101);
dataTable.Rows.Add(row);
return dataTable;
}
2. 创建第2个数据表DataTable
private DataTable MakeOrdersDataTable(DataTable v_customersDataTable)
{
// Declare a DataTable to contain the program generated data
DataTable dataTable = new DataTable("Orders");
// Create and add a CustomerID column
DataColumn colWork = new DataColumn("CustomerID", System.Type.GetType("System.Int32"));
dataTable.Columns.Add(colWork);
// Add CustomerID column to key array
DataColumn[] Keys = new DataColumn[2];
Keys[0] = colWork;
// Create and add OrderID column
colWork = new DataColumn("OrderID", System.Type.GetType("System.String"));
colWork.MaxLength = 15;
dataTable.Columns.Add(colWork);
// Add OrderID column to key array and bind to DataTable
Keys[1] = colWork;
dataTable.PrimaryKey = Keys;
// Create and add a EmployeeID column
colWork = new DataColumn("EmployeeID", System.Type.GetType("System.Int32"));
dataTable.Columns.Add(colWork);
// Create and add a OrderDate column
colWork = new DataColumn("OrderDate", System.Type.GetType("System.DateTime"));
dataTable.Columns.Add(colWork);
// Loop through Customer table and add Order rows
foreach( DataRow custRow in v_customersDataTable.Rows)
{
// Add four rows for each Customer
DataRow row;
for(Int32 intPtr = 1; intPtr <= 4; intPtr++)
{
row = dataTable.NewRow();
row["CustomerID"] = custRow["CustomerID"];
row["OrderID"] = intPtr * ((Int32)custRow["CustomerID"]);
row["EmployeeID"] = intPtr * 10;
row["OrderDate"] = System.DateTime.Now.AddDays(intPtr);
dataTable.Rows.Add(row);
}
}
return dataTable;
}
3. 在按钮单击事件里创建一个DataSet并绑定到UltraGrid
private void ultraButton1_Click(object sender, EventArgs e)
{
// Declare DataSet to contain Hierarchical data
DataSet dataSet = new DataSet();
// Make Customers DataTable
dataSet.Tables.Add(MakeCustomersDataTable());
// Make Orders DataTable
dataSet.Tables.Add(MakeOrdersDataTable(dataSet.Tables["Customers"]));
// Create customers/orders relationship and add to DataSet
DataRelation relCustOrder = new DataRelation("CustOrder"
, dataSet.Tables["Customers"].Columns["CustomerID"]
, dataSet.Tables["Orders"].Columns["CustomerID"]);
dataSet.Relations.Add(relCustOrder);
// Bind the DataSet to the Grid
this.ultraGrid1.DataSource = dataSet;
}