一. 控件说明: XtraPivotGridControl;数据控件
二. 控件特点:
1. 支持行, 列字段拖动, 对调
支持行, 列字段的添加, 移除
支持数据字段的添加, 移除, 对调
2. 支持以行, 列字段排序
支持以过滤字段排序
3. 支持行, 列字段过滤
4. 支持行, 列字段的归类, 即形成父子层
横向和竖向同时形成树结构, 可收缩展开
三.测试数据: 1.数据库: sqlserver2000; Norwind
2.关联表: Customers; Orders; Order_Details; Products;
3.DevExpress 版本: V8.1
四. 运行截图:
1. 行字段: 客户名称;订单号
列字段: 产品名称
数据字段: 商品单价;购买数量
过滤字段: 客户公司名称;所签合同名;订购日期;截止日期;提货日期
2. 以客户名称进行过滤
3. 拖动行字段和列字段(此处演示对调)
产品名称作为行, 客户名称作为列显示
同时将过滤区的字段 订购日期作加入列中
这样重构树: 客户名称à订购日期
数据字段移除商品单价, 只保留购买数量
五. 程序代码
1. 此处全部以 RunTime 进行设计
2. 绑定数据源:
(1):selectSql:
this.sqlstr = "select a.*, b.*, c.*, d.* from Orders a inner join Order_Details b on a.OrderID=b.OrderID "
+ " inner join Customers c on a.CustomerID=c.CustomerID inner join Products d on b.ProductID=d.ProductID;";
(2): DataSource ; DataMember;
this.pivotGridControl1.DataSource = this.database.RunReturnDataSet(this.sqlstr);
this.pivotGridControl1.DataMember = this.database.dataSet.Tables[0].TableName;
3. 运行时添加字段
子逸制作--动态添加字段
/// <summary>
/// 给消费链添加添加字段
/// </summary>
private void AddOrdersColumns()
{
//Customers表
PivotGridField customerID = new PivotGridField();
customerID.FieldName = "CustomerID";
customerID.Caption = "客户名称";
customerID.Area = DevExpress.XtraPivotGrid.PivotArea.RowArea;
PivotGridField companyName = new PivotGridField();
companyName.FieldName = "CompanyName";
companyName.Caption = "客户公司名称";
companyName.Area = DevExpress.XtraPivotGrid.PivotArea.FilterArea;
companyName.Width = 150;
PivotGridField contactTitle = new PivotGridField();
contactTitle.FieldName = "ContactTitle";
contactTitle.Caption = "所签合同名";
contactTitle.Area = DevExpress.XtraPivotGrid.PivotArea.FilterArea;
//Orders表
PivotGridField orderID = new PivotGridField();
orderID.FieldName = "OrderID";
orderID.Caption = "订单号";
orderID.Area = DevExpress.XtraPivotGrid.PivotArea.RowArea;
PivotGridField orderDate = new PivotGridField();
orderDate.FieldName = "OrderDate";
orderDate.Caption = "订购日期";
orderDate.Area = DevExpress.XtraPivotGrid.PivotArea.FilterArea;
PivotGridField requiredDate = new PivotGridField();
requiredDate.FieldName = "RequiredDate";
requiredDate.Caption = "截止日期";
requiredDate.Area = DevExpress.XtraPivotGrid.PivotArea.FilterArea;
PivotGridField shippedDate = new PivotGridField();
shippedDate.FieldName = "ShippedDate";
shippedDate.Caption = "提货日期";
shippedDate.Area = DevExpress.XtraPivotGrid.PivotArea.FilterArea;
//OrderDetails
PivotGridField unitPrice = new PivotGridField();
unitPrice.FieldName = "UnitPrice";
unitPrice.Caption = "商品单价";
unitPrice.Area = DevExpress.XtraPivotGrid.PivotArea.DataArea;
PivotGridField Quantity = new PivotGridField();
Quantity.FieldName = "Quantity";
Quantity.Caption = "购买数量";
Quantity.Area = DevExpress.XtraPivotGrid.PivotArea.DataArea;
//Product表
PivotGridField productName = new PivotGridField();
productName.FieldName = "ProductName";
productName.Caption = "产品名称";
productName.Area = DevExpress.XtraPivotGrid.PivotArea.ColumnArea;
//添加到控制中
this.pivotGridControl1.Fields.Add(customerID);
this.pivotGridControl1.Fields.Add(companyName);
this.pivotGridControl1.Fields.Add(contactTitle);
this.pivotGridControl1.Fields.Add(orderID);
this.pivotGridControl1.Fields.Add(orderDate);
this.pivotGridControl1.Fields.Add(requiredDate);
this.pivotGridControl1.Fields.Add(shippedDate);
this.pivotGridControl1.Fields.Add(unitPrice);
this.pivotGridControl1.Fields.Add(Quantity);
this.pivotGridControl1.Fields.Add(productName);
}
2. 运行时改变字段的显示区域
customerID.Area = DevExpress.XtraPivotGrid.PivotArea.RowArea;
五. 属性
1. 允许拖动
this.pivotGridControl1.OptionsCustomization.AllowDrag = true;
2. 允许排序
this.pivotGridControl1.OptionsCustomization.AllowSort = true;
3. 允许过滤
this.pivotGridControl1.OptionsCustomization.AllowFilter = true;
六: 总结后记
1. 此控件较为灵活;对于 Master_Details 关系表处理的相当好;
2. 此控件数据区只能显示数字类型字段, 类似于统计分析;
3. 后面会有更多的 DevExpress 控件介绍给大家
4. 这里只介绍控件的简单使用;如需更复杂的技术支持或交流请与作者联系;
QQ: 915571300 子逸
5. 版权所有: 子逸(博客园);转载请注明出处;