WinForm界面开发小技巧,如何将Pivot Grid用作主筛选器项

点击获取工具>>

先决条件
  • 平台:Business Intelligence
  • 产品:WinForms Dashboard

本文演示如何使用Pivot dashboard项目像单选主过滤器项目一样运行,此方法使用以下功能:按仪表板参数进行过滤、对基础控件的访问,条件格式设置以及用于访问基础数据的API方法。

  1. 在要用于过滤的Pivot item中创建一组与尺寸对应的仪表盘参数,本文使用四个参数:ProductParam、CategoryParam、CountryParam和CityParam,它们对应于按Periods Pivot item的Product Amount行和列部分中使用的Product、Category、Country和City。
  2. 处理DashboardDesigner.DashboardItemClick 事件,来从clicked元素获取尺寸的值,并将其传递给仪表盘参数。

C#

`void OnDashboardItemClick(object sender, DashboardItemMouseActionEventArgs e) {
if(e.DashboardItemName == "pivotDashboardItem1" && !skipFiltering) {
dashboardDesigner1.BeginUpdateParameters();
//clear all parameters
ClearPivotFilter();
//set selected columns and rows to parameters
SetParameterValue(e.GetAxisPoint("Column"));
SetParameterValue(e.GetAxisPoint("Row"));
dashboardDesigner1.EndUpdateParameters();
}
}`

skipFiltering变量指示是否按下Expand / Collapse 按钮,在这种情况下,该变量用于跳过设置过滤器。

  1. 上面的代码段未设置或清除单击元素无法访问的尺寸值,您需要设置仅与所选相交点对应的参数值,并在设置新的值之前清除仪表盘参数中的先前值。

创建以下用于清除参数值的方法:

C#

`void ClearPivotFilter() {
PivotDashboardItem pivotItem = dashboardDesigner1.Dashboard.Items["pivotDashboardItem1"] as PivotDashboardItem;
ClearParameters(pivotItem.Columns);
ClearParameters(pivotItem.Rows);
}

void ClearParameters(DimensionCollection dimensions) {
foreach(var dimension in dimensions)
dashboardDesigner1.Parameters[GetParameterDataMember(dimension.DataMember)].SelectedValue = null;
}`

  1. 当最终用户单击行或列中的 Expand / Collapse按钮时,Dashboard Designer将引发DashboardDesigner.DashboardItemClick事件,处理DashboardDesigner.DashboardItemControlCreated事件来在这种情况下跳过设置过滤器。

C#

`bool skipFiltering = false;
private void dashboardDesigner1_DashboardItemControlCreated(object sender, DevExpress.DashboardWin.DashboardItemControlEventArgs e) {
if(e.DashboardItemName == "pivotDashboardItem1") {
e.PivotGridControl.MouseDown += PivotGridControl_MouseDown;
}
}

private void PivotGridControl_MouseDown(object sender, MouseEventArgs e) {
PivotGridControl pivot = sender as PivotGridControl;
PivotGridHitInfo hi = pivot.CalcHitInfo(e.Location);
skipFiltering = (hi.ValueInfo != null && hi.ValueInfo.ValueHitTest == PivotGridValueHitTest.ExpandButton);
}`

  1. 要通过与Pivot Item相关的仪表盘参数筛选其仪表盘项时,请向这些项添加所有必须的尺寸。如果不想向最终用户显示尺寸,请将尺寸放置到"Hidden Dimensions" 部分。

然后将以下过滤器表达式添加到仪表盘项目:

Code

([Dimension1] = ?Dimension1Param Or ?Dimension1Param Is Null) And
([Dimension2] = ?Dimension2Param Or ?Dimension2Param Is Null) And
...
([DimensionN] = ?DimensionNParam Or ?DimensionNParam Is Null)

本示例使用以下表达式:

Code

([Product] = ?ProductParam Or ?ProductParam Is Null)
And ([Category] = ?CategoryParam Or ?CategoryParam Is Null)
And ([Country] = ?CountryParam Or ?CountryParam Is Null)
And ([City] = ?CityParam Or ?CityParam Is Null)

上面的步骤足以实现最少的过滤功能,进一步的步骤将改善UI操作。

  1. Optional. To 突出显示在Pivot Grid中选择的单元格,请使用以下表达式添加格式规则:

(?CityParam Is Not Null Or ?CountryParam Is Not Null Or ?ProductParam Is Not Null Or ?CategoryParam Is Not Null) And ([Product] = ?ProductParam Or ?ProductParam Is Null) And ([Category] = ?CategoryParam Or ?CategoryParam Is Null) And ([Country] = ?CountryParam Or ?CountryParam Is Null) And ([City] = ?CityParam Or ?CityParam Is Null)

  1. Optional. 处理DashboardDesigner.CustomizeDashboardItemCaption事件,将Clear Master Filter按钮添加到该项目的标题:

C#

`void OnCustomizeDashboardItemCaption(object sender, CustomizeDashboardItemCaptionEventArgs e) {
if(e.DashboardItemName == "pivotDashboardItem1") {
DashboardToolbarItem showDataItem = new DashboardToolbarItem("Clear Master Filter",
new Action((args) => {
dashboardDesigner1.BeginUpdateParameters();
ClearPivotFilter();
dashboardDesigner1.EndUpdateParameters();
}));
showDataItem.Enabled = IsAnyFilterSet();
showDataItem.SvgImage = svgImageCollection1[0];
e.Items.Insert(0, showDataItem);
}
}`

你可能感兴趣的