Loading...
Loading...
Guide for implementing Syncfusion Windows Forms Pivot Grid control for data analysis and pivot table functionality. Use this skill when implementing pivot tables, data summarization, cross-tabulated data, or analytical dashboards in Windows Forms applications. Covers data binding, pivot configuration, filtering, sorting, grouping, calculations, conditional formatting, and exporting.
npx skill4agent add syncfusion/winforms-ui-components-skills syncfusion-winforms-pivot-gridSyncfusion.Windows.Forms.PivotAnalysisSyncfusion.Grid.Windows.dllSyncfusion.PivotAnalysis.Base.dllSyncfusion.PivotAnalysis.Windows.dllSyncfusion.Shared.Base.dllnew PivotGridControl(this.components);LocationSizeShowPivotTableFieldListShowPivotTableFieldList = trueusing System;
using System.Collections.Generic;
using System.Windows.Forms;
using Syncfusion.Windows.Forms.PivotAnalysis;
using Syncfusion.PivotAnalysis.Base;
using Syncfusion.Windows.Forms;
namespace PivotGridDemo
{
public partial class MainForm : Form
{
private PivotGridControl pivotGridControl1;
public MainForm()
{
InitializeComponent();
InitializePivotGrid();
}
private void InitializePivotGrid()
{
// Create pivot grid instance using the form components container
pivotGridControl1 = new PivotGridControl(this.components);
// Set size and position first
pivotGridControl1.Location = new System.Drawing.Point(10, 10);
pivotGridControl1.Size = new System.Drawing.Size(800, 500);
pivotGridControl1.Anchor = AnchorStyles.Top | AnchorStyles.Left |
AnchorStyles.Right | AnchorStyles.Bottom;
// Apply visual style
pivotGridControl1.GridVisualStyles = GridVisualStyles.Metro;
// Bind data source
pivotGridControl1.ItemSource = GetSalesData();
// Configure pivot rows
pivotGridControl1.PivotRows.Add(new PivotItem
{
FieldMappingName = "Product",
TotalHeader = "Total"
});
pivotGridControl1.PivotRows.Add(new PivotItem
{
FieldMappingName = "Date",
TotalHeader = "Total"
});
// Configure pivot columns
pivotGridControl1.PivotColumns.Add(new PivotItem
{
FieldMappingName = "Country",
TotalHeader = "Total"
});
// Configure calculations (summary values)
pivotGridControl1.PivotCalculations.Add(new PivotComputationInfo
{
FieldName = "Amount",
Format = "C", // Currency format
SummaryType = SummaryType.DoubleTotalSum
});
pivotGridControl1.PivotCalculations.Add(new PivotComputationInfo
{
FieldName = "Quantity",
Format = "#,##0"
});
// Enable the field list only after the control has been sized correctly
pivotGridControl1.ShowPivotTableFieldList = true;
// Add to form
this.Controls.Add(pivotGridControl1);
}
private List<ProductSales> GetSalesData()
{
// Sample data for demonstration
return new List<ProductSales>
{
new ProductSales
{
Product = "Bike",
Date = "FY 2023",
Country = "United States",
Quantity = 120,
Amount = 24000
},
new ProductSales
{
Product = "Car",
Date = "FY 2023",
Country = "Canada",
Quantity = 45,
Amount = 135000
},
// Add more data...
};
}
}
public class ProductSales
{
public string Product { get; set; }
public string Date { get; set; }
public string Country { get; set; }
public int Quantity { get; set; }
public double Amount { get; set; }
}
}using Syncfusion.Windows.Forms.PivotAnalysis;
// Show the Pivot Table Field List (schema designer)
pivotGridControl1.ShowPivotTableFieldList = true;
// Users can now drag and drop fields to reorganize the pivot structure
// This provides Excel-like field management functionalityusing Syncfusion.Windows.Forms.PivotAnalysis;
// Add conditional formatting rule
PivotCellStyle style = new PivotCellStyle();
style.BackColor = Color.LightCoral;
style.ForeColor = Color.White;
ConditionalFormat condition = new ConditionalFormat();
condition.Conditions.Add(new PivotCondition
{
ConditionType = PivotConditionType.LessThan,
PredicateValue = 10000
});
condition.ApplyStyleInfo = style;
pivotGridControl1.TableControl.ConditionalFormats.Add(condition);using Syncfusion.Windows.Forms.PivotAnalysis;
// Export pivot grid to Excel file
pivotGridControl1.ExportToExcel("PivotReport.xlsx");
// Export with options
ExcelExportOptions options = new ExcelExportOptions();
options.ExportMode = ExportMode.Value; // or ExportMode.Text
pivotGridControl1.ExportToExcel("PivotReport.xlsx", options);using Syncfusion.Windows.Forms.PivotAnalysis;
// Subscribe to drill-down event
pivotGridControl1.HyperlinkCellClick += PivotGridControl1_HyperlinkCellClick;
private void PivotGridControl1_HyperlinkCellClick(object sender,
HyperlinkCellClickEventArgs e)
{
// Get the clicked cell information
string cellValue = e.Text;
int rowIndex = e.RowIndex;
int colIndex = e.ColIndex;
// Show drill-through data or navigate to details
// e.Cancel = true; // Cancel default drill behavior if needed
}using Syncfusion.Windows.Forms.PivotAnalysis;
using Syncfusion.PivotAnalysis.Base;
// Add filter to a specific field
PivotItem productRow = pivotGridControl1.PivotRows[0];
productRow.FilterItems = new List<string> { "Bike", "Car" };
// Or use filter expressions
FilterExpression filter = new FilterExpression();
filter.FieldName = "Product";
filter.FilterItems.Add("Bike");
filter.FilterItems.Add("Car");
pivotGridControl1.Filters.Add(filter);
// Refresh the pivot grid to apply filters
pivotGridControl1.TableControl.Refresh();using Syncfusion.PivotAnalysis.Base;
// Create custom calculation with expression
PivotComputationInfo customCalc = new PivotComputationInfo
{
FieldName = "Profit",
CalculationType = CalculationType.Custom,
Format = "C",
SummaryType = SummaryType.Custom
};
// Define custom summary calculation
customCalc.CustomSummaryMethod = (items) =>
{
double total = 0;
foreach (var item in items)
{
total += (double)item.GetType().GetProperty("Amount").GetValue(item, null);
}
return total * 0.15; // Calculate 15% profit margin
};
pivotGridControl1.PivotCalculations.Add(customCalc);ItemSourcePivotRowsPivotColumnsPivotCalculationsShowPivotTableFieldListGridVisualStylesEnableDrillDownShowGrandTotalsShowSubTotalsAllowSelectionAllowFilteringAllowSorting