Back to Devexpress

XRPivotGridField Class

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-pivotgrid-ebe72b69.md

latest8.1 KB
Original Source

XRPivotGridField Class

Represents a field within the XRPivotGrid control.

Namespace : DevExpress.XtraReports.UI.PivotGrid

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public class XRPivotGridField :
    PivotGridFieldBase,
    IPivotGridDataContainer,
    IPivotGridDataContainerCore
vb
Public Class XRPivotGridField
    Inherits PivotGridFieldBase
    Implements IPivotGridDataContainer,
               IPivotGridDataContainerCore

The following members return XRPivotGridField objects:

Remarks

This class is inherited from the PivotGridField class, and replicates all its functionality. The main purpose of this class is to hide the PivotGridField.Appearance property, because the field’s appearance should be specified using the XRPivotGrid.Appearance or XRControl.Styles property of an XRPivotGrid control.

For more information on using pivot grid fields, refer to the PivotGridField class description.

Example

The following code generates a cross-tab report by utilizing the XRPivotGrid control.

Before running this code, add all necessary assemblies to the project’s References list (e.g., DevExpress.XtraPivotGrid.v25.2.dll and DevExpress.PivotGrid.v25.2.Core.dll ).

Next, add a button ( button1 ) to the application’s form and handle its Click event as follows.

csharp
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UI.PivotGrid;
// ...
private void button1_Click(object sender, EventArgs e) {
    // Create a cross-tab report.
    XtraReport report = CreateReport();

    // Show its Print Preview.
    report.ShowPreview();
}

private XtraReport CreateReport() {
    // Create a blank report.
    XtraReport crossTabReport = new XtraReport();

    // Create a detail band and add it to the report.
    DetailBand detail = new DetailBand();
    crossTabReport.Bands.Add(detail);

    // Create a pivot grid and add it to the Detail band.
    XRPivotGrid pivotGrid = new XRPivotGrid();
    detail.Controls.Add(pivotGrid);

    // Create a data source
    SQLiteConnectionParameters connectionParameters = new SQLiteConnectionParameters() {
                    FileName = "nwind.db",
                    Password = null
                };
    SqlDataSource ds = new SqlDataSource(connectionParameters);

    // Create an SQL query to access the SalesPerson view.
    CustomSqlQuery query = new CustomSqlQuery();
    query.Name = "SalesPerson";
    query.Sql = "SELECT CategoryName, ProductName, Country, [Sales Person], Quantity, [Extended Price] FROM SalesPerson";
    ds.Queries.Add(query);

    // Bind the pivot grid to data.
    pivotGrid.DataSource = ds;
    pivotGrid.DataMember = "SalesPerson";

    // Generate pivot grid's fields.
    XRPivotGridField fieldCategoryName = new XRPivotGridField("CategoryName", PivotArea.RowArea);
    XRPivotGridField fieldProductName = new XRPivotGridField("ProductName", PivotArea.RowArea);
    XRPivotGridField fieldCountry = new XRPivotGridField("Country", PivotArea.ColumnArea);
    XRPivotGridField fieldSalesPerson = new XRPivotGridField("Sales Person", PivotArea.ColumnArea);
    XRPivotGridField fieldQuantity = new XRPivotGridField("Quantity", PivotArea.DataArea);
    XRPivotGridField fieldExtendedPrice = new XRPivotGridField("Extended Price", PivotArea.DataArea);

    // Add these fields to the pivot grid.
    pivotGrid.Fields.AddRange(new XRPivotGridField[] {fieldCategoryName, fieldProductName, fieldCountry, 
        fieldSalesPerson, fieldQuantity, fieldExtendedPrice});

    return crossTabReport;
}
vb
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraPivotGrid
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraReports.UI.PivotGrid
' ...
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    ' Create a cross-tab report.
    Dim report As XtraReport = CreateReport()

    ' Show its Print Preview.
    report.ShowPreview()
End Sub

Private Function CreateReport() As XtraReport
    ' Create a blank report.
    Dim crossTabReport As New XtraReport()

    ' Create a detail band and add it to the report.
    Dim detail As New DetailBand()
    crossTabReport.Bands.Add(detail)

    ' Create a pivot grid and add it to the Detail band.
    Dim pivotGrid As New XRPivotGrid()
    detail.Controls.Add(pivotGrid)

    ' Create a data source
    Dim connectionParameters As New SQLiteConnectionParameters() With {
                .FileName = "nwind.db",
                .Password = Nothing
            }
    Dim ds As New SqlDataSource(connectionParameters)

    ' Create an SQL query to access the SalesPerson view.
    Dim query As New CustomSqlQuery()
    query.Name = "SalesPerson"
    query.Sql = "SELECT CategoryName, ProductName, Country, [Sales Person], Quantity, [Extended Price] FROM SalesPerson"
    ds.Queries.Add(query)

    ' Bind the pivot grid to data.
    pivotGrid.DataSource = ds
    pivotGrid.DataMember = "SalesPerson"

    ' Generate pivot grid's fields.
    Dim fieldCategoryName As New XRPivotGridField("CategoryName", PivotArea.RowArea)
    Dim fieldProductName As New XRPivotGridField("ProductName", PivotArea.RowArea)
    Dim fieldCountry As New XRPivotGridField("Country", PivotArea.ColumnArea)
    Dim fieldSalesPerson As New XRPivotGridField("Sales Person", PivotArea.ColumnArea)
    Dim fieldQuantity As New XRPivotGridField("Quantity", PivotArea.DataArea)
    Dim fieldExtendedPrice As New XRPivotGridField("Extended Price", PivotArea.DataArea)

    ' Add these fields to the pivot grid.
    pivotGrid.Fields.AddRange(New XRPivotGridField() {fieldCategoryName, fieldProductName, fieldCountry, fieldSalesPerson, fieldQuantity, fieldExtendedPrice})

    Return crossTabReport
End Function

Inheritance

Object MarshalByRefObject Component PivotGridFieldBase XRPivotGridField

See Also

XRPivotGridField Members

DevExpress.XtraReports.UI.PivotGrid Namespace