PivotTable.ChangeDataSource(Range) Method
Changes the data source for the PivotTable report.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Spreadsheet.v19.1.Core.dll
Declaration
Parameters
Name | Type | Description |
---|---|---|
sourceRange | Range | A Range 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.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-spreadsheet-pivot-table-api-t298650
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet
' Access the pivot table by its name in the collection.
Dim pivotTable As PivotTable = worksheet.PivotTables("PivotTable1")
Dim sourceWorksheet As Worksheet = 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.
Dim dataField As PivotDataField = pivotTable.DataFields.Add(pivotTable.Fields("Yearly Earnings"))
' Calculate the average of the "Yearly Earnings" values for each state.
dataField.SummarizeValuesBy = PivotDataConsolidationFunction.Average
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the ChangeDataSource(Range) 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.