Back to Devexpress

PivotCellEventArgsBase<TField, TData, TCustomTotal>.GetColumnFields() Method

corelibraries-devexpress-dot-xtrapivotgrid-dot-pivotcelleventargsbase-3-b9a06b58.md

latest4.6 KB
Original Source

PivotCellEventArgsBase<TField, TData, TCustomTotal>.GetColumnFields() Method

Returns an array of column fields that correspond to the current cell.

Namespace : DevExpress.XtraPivotGrid

Assembly : DevExpress.PivotGrid.v25.2.Core.dll

NuGet Packages : DevExpress.PivotGrid.Core, DevExpress.Win.Navigation

Declaration

csharp
public TField[] GetColumnFields()
vb
Public Function GetColumnFields As TField()

Returns

TypeDescription
TField[]

An array of column fields.

|

Remarks

The PivotGrid control supports multiple column fields. Use the GetColumnFields method to get column fields that correspond to the processed cell. If the cell belongs to a Column Grand Total, the GetColumnFields method returns an empty array (the array’s length is 0).

Use the e.ColumnField property to get the innermost column field that corresponds to the current cell. This property’s value matches the value of the last element in the array returned by the GetColumnFields method.

Use the e.GetFieldValue method to get any field’s value for the processed cell.

Example

This example calculates percentage based on the Beverages row value for each column. The Pivot Grid handles the PivotGridControl.CustomCellValue event to display a calculated percentage value in the cell that belongs to the % of Beverages Sales column. The grand total values are hidden.

View Example

cs
private void pivotGridControl1_CustomCellValue(object sender, PivotCellValueEventArgs e) {
    // Calculates the 'Percent' field values.
    if (e.DataField.Name == "PercentOfBeverages") {
        // Hides grand total values.
        if (e.RowValueType == PivotGridValueType.GrandTotal) {
            e.Value = null;
            return;
        }
        var rowValues = e.GetRowFields().Select(f => 
            f == fieldCategoryName ? "Beverages" : e.GetFieldValue(f)).ToArray();
        var columnValues = e.GetColumnFields().Select(f => 
            f == fieldCategoryName ? "Beverages" : e.GetFieldValue(f)).ToArray();
        decimal beveragesValue = Convert.ToDecimal(e.GetCellValue(columnValues, rowValues, e.DataField));
        if (beveragesValue == 0)
            e.Value = null;
        else
            e.Value = Convert.ToDecimal(e.Value) / beveragesValue;
    }
    else return;
}
vb
Private Sub pivotGridControl1_CustomCellValue(ByVal sender As Object, ByVal e As PivotCellValueEventArgs) Handles pivotGridControl1.CustomCellValue
    ' Calculates the 'Percent' field values.
    If e.DataField.Name = "PercentOfBeverages" Then
        ' Do not display grand total values.
        If e.RowValueType = PivotGridValueType.GrandTotal Then
            e.Value = Nothing
            Return
        End If

        Dim rowValues = e.GetRowFields().Select(Function(f) If(f Is fieldCategoryName, "Beverages", e.GetFieldValue(f))).ToArray()
        Dim columnValues = e.GetColumnFields().Select(Function(f) If(f Is fieldCategoryName, "Beverages", e.GetFieldValue(f))).ToArray()
        Dim beveragesValue As Decimal = Convert.ToDecimal(e.GetCellValue(columnValues, rowValues, e.DataField))
        If beveragesValue = 0 Then
            e.Value = Nothing
        Else
            e.Value = Convert.ToDecimal(e.Value) / beveragesValue
        End If
    Else
        Return
    End If

See Also

PivotCellEventArgsBase<TField, TData, TCustomTotal> Class

PivotCellEventArgsBase<TField, TData, TCustomTotal> Members

DevExpress.XtraPivotGrid Namespace