界面開發(fā)工具DevExpress WinForms使用教程:將Pivot Grid用作主篩選器項
下載DevExpress v20.2完整版 DevExpress v20.2漢化資源獲取
DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!體驗?點(diǎn)擊下載>>
先決條件
- 平臺:Business Intelligence
- 產(chǎn)品:WinForms Dashboard
本文演示如何使用Pivot dashboard項目像單選主過濾器項目一樣運(yùn)行,此方法使用以下功能:按儀表板參數(shù)進(jìn)行過濾、對基礎(chǔ)控件的訪問,條件格式設(shè)置以及用于訪問基礎(chǔ)數(shù)據(jù)的API方法。
1. 在要用于過濾的Pivot item中創(chuàng)建一組與尺寸對應(yīng)的儀表盤參數(shù),本文使用四個參數(shù):ProductParam、CategoryParam、CountryParam和CityParam,它們對應(yīng)于按Periods Pivot item的Product Amount行和列部分中使用的Product、Category、Country和City。
2. 處理DashboardDesigner.DashboardItemClick 事件,來從clicked元素獲取尺寸的值,并將其傳遞給儀表盤參數(shù)。
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 按鈕,在這種情況下,該變量用于跳過設(shè)置過濾器。
3. 上面的代碼段未設(shè)置或清除單擊元素?zé)o法訪問的尺寸值,您需要設(shè)置僅與所選相交點(diǎn)對應(yīng)的參數(shù)值,并在設(shè)置新的值之前清除儀表盤參數(shù)中的先前值。
創(chuàng)建以下用于清除參數(shù)值的方法:
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; }
4. 當(dāng)最終用戶單擊行或列中的 Expand / Collapse按鈕時,Dashboard Designer將引發(fā)DashboardDesigner.DashboardItemClick事件,處理DashboardDesigner.DashboardItemControlCreated事件來在這種情況下跳過設(shè)置過濾器。
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); }
5. 要通過與Pivot Item相關(guān)的儀表盤參數(shù)篩選其儀表盤項時,請向這些項添加所有必須的尺寸。如果不想向最終用戶顯示尺寸,請將尺寸放置到"Hidden Dimensions" 部分。
然后將以下過濾器表達(dá)式添加到儀表盤項目:
Code
([Dimension1] = ?Dimension1Param Or ?Dimension1Param Is Null) And
([Dimension2] = ?Dimension2Param Or ?Dimension2Param Is Null) And
...
([DimensionN] = ?DimensionNParam Or ?DimensionNParam Is Null)
本示例使用以下表達(dá)式:
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)
上面的步驟足以實現(xiàn)最少的過濾功能,進(jìn)一步的步驟將改善UI操作。
1. Optional. To 突出顯示在Pivot Grid中選擇的單元格,請使用以下表達(dá)式添加格式規(guī)則:
(?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)
2. Optional. 處理DashboardDesigner.CustomizeDashboardItemCaption事件,將Clear Master Filter按鈕添加到該項目的標(biāo)題:
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); } }
上DevExpress中文網(wǎng),獲取第一手最新產(chǎn)品資訊!
DevExpress技術(shù)交流群2:775869749 歡迎一起進(jìn)群討論