How to: Locate a Column (Row) Header By Its Column's (Row's) Summary Values
3 minutes to read
The following example demonstrates how to handle the CustomFieldValueCells event to locate a specific column/row header identified by its column's/row's summary values.
In this example, a predicate is used to locate a column that contains only zero summary values. The column header is obtained by the event parameter's FindCell method, and then removed via the Remove method.
usingSystem;
usingSystem.Globalization;
usingSystem.Web.UI;
usingDevExpress.Web.ASPxPivotGrid;
usingDevExpress.XtraPivotGrid;
namespaceASPxPivotGrid_FindCells {
publicpartialclass _Default : Page {
protectedvoidPage_Load(object sender, EventArgs e) {
if (!IsCallback && !IsPostBack) {
PivotHelper.FillPivot(pivotGrid);
}
pivotGrid.DataSource = PivotHelper.GetDataTable();
}
// Handles the CustomFieldValueCells event to remove columns with// zero summary values.protectedvoidpivotGrid_CustomFieldValueCells(object sender,
PivotCustomFieldValueCellsEventArgs e) {
if (pivotGrid.DataSource == null) return;
if (radioButtonList.SelectedIndex == 0) return;
// Obtains the first encountered column header whose column// matches the specified condition, represented by a predicate.
FieldValueCell cell = e.FindCell(true, new Predicate<object[]>(
// Defines the predicate returning true for columns// that contain only zero summary values.delegate(object[] dataCellValues) {
foreach (objectvaluein dataCellValues) {
if (!object.Equals((decimal)0, value))
returnfalse;
}
returntrue;
}));
// If any column header matches the condition, this column is removed.if (cell != null) e.Remove(cell);
}
protectedvoidpivotGrid_FieldValueDisplayText(object sender,
PivotFieldDisplayTextEventArgs e) {
if (e.Field == pivotGrid.Fields[PivotHelper.Month]) {
e.DisplayText = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName((int)e.Value);
}
}
}
}
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="ASPxPivotGrid_FindCells._Default" %><%@RegisterAssembly="DevExpress.Web.ASPxPivotGrid.v10.2, Version=10.2.3.0,
Culture=neutral, PublicKeyToken=b88d1754d700e49a"Namespace="DevExpress.Web.ASPxPivotGrid"TagPrefix="dx" %><!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml" ><headid="Head1"runat="server"><title>Untitled Page</title></head><body><formid="form1"runat="server"><asp:RadioButtonListID="radioButtonList"runat="server"AutoPostBack="true"CellSpacing="10" ><asp:ListItemSelected="True">Default Layout</asp:ListItem><asp:ListItem>Remove Columns with Zero Summary Values</asp:ListItem></asp:RadioButtonList><br /><div><dx:ASPxPivotGridID="pivotGrid"runat="server"Width="500px"OnFieldValueDisplayText="pivotGrid_FieldValueDisplayText"OnCustomFieldValueCells="pivotGrid_CustomFieldValueCells"OptionsCustomization-AllowFilter="false"OptionsCustomization-AllowDrag="false"></dx:ASPxPivotGrid></div></form></body></html>
<%@PageLanguage="vb"AutoEventWireup="true"CodeBehind="Default.aspx.vb"Inherits="ASPxPivotGrid_FindCells._Default" %><%@RegisterAssembly="DevExpress.Web.ASPxPivotGrid.v10.2, Version=10.2.3.0,
Culture=neutral, PublicKeyToken=b88d1754d700e49a"Namespace="DevExpress.Web.ASPxPivotGrid"TagPrefix="dx" %><!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml" ><headid="Head1"runat="server"><title>Untitled Page</title></head><body><formid="form1"runat="server"><asp:RadioButtonListID="radioButtonList"runat="server"AutoPostBack="true"CellSpacing="10" ><asp:ListItemSelected="True">Default Layout</asp:ListItem><asp:ListItem>Remove Columns with Zero Summary Values</asp:ListItem></asp:RadioButtonList><br /><div><dx:ASPxPivotGridID="pivotGrid"runat="server"Width="500px"OnFieldValueDisplayText="pivotGrid_FieldValueDisplayText"OnCustomFieldValueCells="pivotGrid_CustomFieldValueCells"OptionsCustomization-AllowFilter="false"OptionsCustomization-AllowDrag="false"></dx:ASPxPivotGrid></div></form></body></html>
ImportsMicrosoft.VisualBasicImportsSystemImportsSystem.GlobalizationImportsSystem.Web.UIImportsDevExpress.Web.ASPxPivotGridImportsDevExpress.XtraPivotGridNamespace ASPxPivotGrid_FindCells
PartialPublicClass _Default
Inherits Page
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As EventArgs)
If (Not IsCallback) AndAlso (Not IsPostBack) Then
PivotHelper.FillPivot(pivotGrid)
EndIf
pivotGrid.DataSource = PivotHelper.GetDataTable()
EndSub' Handles the CustomFieldValueCells event to remove columns with' zero summary values.ProtectedSub pivotGrid_CustomFieldValueCells(ByVal sender AsObject, _
ByVal e As PivotCustomFieldValueCellsEventArgs)
If pivotGrid.DataSource IsNothingThenReturnEndIfIf radioButtonList.SelectedIndex = 0ThenReturnEndIf' Obtains the first encountered column header whose column' matches the specified condition, represented by a predicate.Dim cell As FieldValueCell = _
e.FindCell(True, New Predicate(OfObject())(AddressOf AnonymousMethod1))
' If any column header matches the condition, this column is removed.If cell IsNotNothingThen
e.Remove(cell)
EndIfEndSub' Defines the predicate returning true for columns' that contain only zero summary values.PrivateFunction AnonymousMethod1(ByVal dataCellValues() AsObject) AsBooleanForEach value AsObjectIn dataCellValues
If (NotObject.Equals(CDec(0), value)) ThenReturnFalseEndIfNext value
ReturnTrueEndFunctionProtectedSub pivotGrid_FieldValueDisplayText(ByVal sender AsObject, _
ByVal e As PivotFieldDisplayTextEventArgs)
If e.Field Is pivotGrid.Fields(PivotHelper.Month) Then
e.DisplayText = _
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(CInt(Fix(e.Value)))
EndIfEndSubEndClassEndNamespace
Was this page helpful?
Thanks for your feedback!
How can we improve this help topic?
Additional comments/thoughts:
If you have any questions, submit a ticket to our Support Center.