Skip to main content
All docs
V25.1
  • Row

    PivotTable.ChangeDataSource(CellRange) Method

    Changes the data source for the PivotTable report.

    Namespace: DevExpress.Spreadsheet

    Assembly: DevExpress.Spreadsheet.v25.1.Core.dll

    NuGet Package: DevExpress.Spreadsheet.Core

    Declaration

    void ChangeDataSource(
        CellRange sourceRange
    )

    Parameters

    Name Type Description
    sourceRange CellRange

    A CellRange object that specifies a cell range containing new source data for the pivot table.

    Remarks

    After you create a pivot table, you can change the cell range containing its source data. For example, you can update your report to include new rows or columns that have been added to the initial source range, or rebuild your report based on completely new data, as shown in the example below. In the latter case, you should re-create the pivot table and fill it with data by adding necessary fields to it.

    View Example

    Worksheet worksheet = workbook.Worksheets["Report1"];
    workbook.Worksheets.ActiveWorksheet = worksheet;
    
    // Access the pivot table by its name in the collection.
    PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
    Worksheet sourceWorksheet = workbook.Worksheets["Data2"];
    // Change the data source of the pivot table.
    pivotTable.ChangeDataSource(sourceWorksheet["A1:H6367"]);
    
    // Add the "State" field to the row axis area.
    pivotTable.RowFields.Add(pivotTable.Fields["State"]);
    // Add the "Yearly Earnings" field to the data area.
    PivotDataField dataField = pivotTable.DataFields.Add(pivotTable.Fields["Yearly Earnings"]);
    // Calculate the average of the "Yearly Earnings" values for each state.
    dataField.SummarizeValuesBy = PivotDataConsolidationFunction.Average;
    

    The following code snippets (auto-collected from DevExpress Examples) contain references to the ChangeDataSource(CellRange) method.

    Note

    The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

    See Also