关于 syncfusion-winforms-pivot-grid
Syncfusion Windows Forms Pivot Grid 是用于 Windows Forms 应用的数据分析控件,支持交互式透视布局、计算、筛选及多种数据源的导出。
主要功能
- 01拖放式透视字段编排
- 02分层下钻与上钻
- 03内置汇总和自定义计算
- 04筛选、排序与条件格式
- 05导出至 Excel、PDF 和 Word
适用场景
- 01按产品、地区和时间分析销售
- 02比较预算与实际财务结果
- 03按地点和类别查看库存
- 04构建支持下钻的 KPI 报表
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ syncfusion/ winforms-ui-components-skills --skill syncfusion-winforms-pivot-grid安装后在 Claude Code 中输入/syncfusion-winforms-pivot-grid确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/advanced-features.mdcalculations-editing.mdcell-selection-formatting.mddata-binding.mddata-exploration.mdexporting-printing.mdfiltering-sorting.mdgetting-started.mdgrouping-layouts.mdpivot-schema-designer.mdrows-columns-summaries.md详细介绍
Syncfusion Windows Forms Pivot Grid
Syncfusion Windows Forms Pivot Grid 控件是一款功能强大的 Excel 风格数据透视表组件,可实现交互式数据分析和交叉表报告。它通过拖放字段组织、下钻导航和高级计算,将大型数据集转换为有意义的摘要。
适用场景
在以下场景中使用此技能:
- 在 Windows Forms 应用程序中实现数据透视表功能
- 构建用于数据汇总的商业智能仪表板
- 创建包含层次分组和计算的分析报告
- 通过下钻/上钻导航实现交互式数据探索
- 显示来自关系型或 OLAP 数据源的交叉表数据
- 支持用户动态重新组织数据字段
- 将数据透视分析导出为 Excel、PDF 或 Word 格式
- 根据单元格值应用条件格式
- 实现自定义聚合和计算字段
- 构建财务报告、销售仪表板或 KPI 跟踪界面
组件概述
主要功能:
- Excel 风格数据透视表 - 提供熟悉的数据透视和分析界面
- 交互式架构设计器 - 内置数据透视表字段列表,支持通过拖放管理字段
- 多种数据源 - 支持 IList、IEnumerable、DataTable、DataView、OLAP
- 下钻导航 - 交互式展开/折叠层次数据
- 高级计算 - 求和、平均值、计数、最小值、最大值、自定义表达式、累计总计
- 筛选和排序 - 标签筛选、值筛选、多级排序
- 分组栏 - 通过拖放直观地组织字段
- 条件格式 - 根据规则和条件设置单元格样式
- 导出功能 - 导出为 Excel、PDF、Word,并保留格式
- 单元格选择 - 支持单选、多选和范围选择模式
- 编辑支持 - 修改单元格值并更新聚合结果
- 触控支持 - 为平板设备提供便于触控的交互方式
命名空间: Syncfusion.Windows.Forms.PivotAnalysis
程序集依赖项:
Syncfusion.Grid.Windows.dllSyncfusion.PivotAnalysis.Base.dllSyncfusion.PivotAnalysis.Windows.dllSyncfusion.Shared.Base.dll
NuGet 指南:
- 安装最新可用版本的 Syncfusion.PivotTable.WinForms
Install-Package Syncfusion.PivotTable.WinForms -Version *
- 尽可能优先使用 NuGet 包引用,而非手动引用 DLL
- 升级前请确认包与项目目标框架兼容
重要的 UI 设置说明:
- 在 WinForms 窗体中使用时,始终通过
new PivotGridControl(this.components);创建控件。 - 始终先设置
Location和Size,并确保控件尺寸大于 300 x 300,然后再启用ShowPivotTableFieldList。 - 只有在正确设置控件尺寸后,才设置
ShowPivotTableFieldList = true。 - 这样可以避免字段列表 UI 在尺寸过小的控件中以无法使用的布局呈现。
文档与导航指南
入门
📄 阅读: references/getting-started.md
- 程序集部署和所需依赖项
- 通过 Visual Studio 设计器添加 Pivot Grid
- 通过代码后台添加 Pivot Grid
- 许可证密钥注册
- 基本控件初始化
- 初始配置和设置
数据管理
📄 阅读: references/data-binding.md
- 绑定到 IEnumerable 集合(List、ObservableCollection)
- 绑定到 DataTable 和 DataView
- 绑定到 OLAP 数据源
- 动态数据更新和刷新
- 数据源要求和最佳实践
- 大型数据集的性能优化
数据透视架构设计器
📄 阅读: references/pivot-schema-designer.md
- 启用数据透视表字段列表
- 通过拖放组织字段
- 字段配置和自定义
- 显示/隐藏架构设计器
- 以编程方式操作字段
- 自定义字段模板
行、列和汇总
📄 阅读: references/rows-columns-summaries.md
- 配置数据透视行(PivotRows 集合)
- 配置数据透视列(PivotColumns 集合)
- 汇总类型(Sum、Average、Count、Min、Max 等)
- 自定义聚合函数
- 计算信息和格式设置
- 层次结构和嵌套
- 总计标题和小计
筛选和排序
📄 阅读: references/filtering-sorting.md
- 筛选类型(标签筛选、值筛选)
- 使用 UI 进行交互式筛选
- 以编程方式配置筛选
- 筛选条件和谓词
- 排序模式(升序、降序、自定义)
- 多级排序策略
- 清除和重置筛选
分组和布局
📄 阅读: references/grouping-layouts.md
- 分组栏功能和可见性
- 布局类型(普通、顶部汇总、无汇总等)
- 下钻和上钻导航
- 展开/折叠行为和事件
- 自定义布局配置
- 分组数据的性能优化
计算和编辑
📄 阅读: references/calculations-editing.md
- 数据透视计算(差值、占比、累计总计等)
- 创建表达式字段和自定义公式
- 以交互方式编辑单元格值
- 以编程方式更新聚合值
- 验证规则和约束
- 计算字段和自定义聚合
单元格选择和格式设置
📄 阅读: references/cell-selection-formatting.md
- 单元格选择模式(单选、范围、多选)
- 选择事件和处理
- 条件格式规则和条件
- 基于单元格值的格式条件
- 设置单元格、行和列的样式
- 自定义单元格外观和模板
数据探索
📄 阅读: references/data-exploration.md
- 用于导航的超链接单元格
- 用于查看详情的钻取功能
- 数据可视化技术
- 交互式导航模式
- 用户交互事件
- 工具提示和单元格值显示
导出和打印
📄 阅读: references/exporting-printing.md
- 导出为 Excel(XLS、XLSX)
- 保留格式导出为 PDF
- 导出为 Word 文档
- 打印配置和预览
- 自定义导出选项和模板
- 高效处理大型导出
高级功能
📄 阅读: references/advanced-features.md
- 大型数据集的异步数据处理
- 数据透视状态的序列化和反序列化
- 冻结行/列标题
- 支持平板设备触控
快速入门示例
绑定数据的基本数据透视网格
using 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; }
}
}
常见模式
模式 1:启用数据透视架构设计器
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 functionality
模式 2:应用条件格式
using Syncfusion.Windows.Forms.PivotAnalysis;
// Add conditional formatting rule
NewRuleConditionalFormat newRule1 = new NewRuleConditionalFormat();
newRule1.RuleType = RuleType.FormatOnlyCellsThatContain;
newRule1.SummaryElement = "Quantity";
ConditionalFormat condition1 = new ConditionalFormat();
condition1.PredicateType = PredicateType.And;
condition1.ConditionType = PivotGridDataConditionType.LessThan;
condition1.StartValue = 30;
condition1.EndValue = 60;
newRule1.Conditions.Add(condition1);
模式 3:导出为 Excel
using Syncfusion.Windows.Forms.PivotAnalysis;
// Export pivot grid to Excel file
pivotGridControl1.ExportToExcel("PivotReport.xlsx");
// Export with options
ExcelExport excelExport = new ExcelExport(this.pivotGridControl1, Syncfusion.XlsIO.ExcelVersion.Excel2010);
excelExport.ExportMode = ExportModes.Cell;
excelExport.Export(@"D:\PivotGrid.xlsx");
模式 4:以编程方式筛选
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();
模式 5:自定义聚合
using Syncfusion.PivotAnalysis.Base;
// Create custom calculation with expression
PivotComputationInfo customCalc = new PivotComputationInfo
{
FieldName = "Profit",
CalculationType = CalculationType.Formula,
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);
常见用例
-
销售分析仪表板
- 按产品、地区和时间段跟踪销售情况
- 跨多个维度比较业绩
- 识别销售数据中的趋势和模式
-
财务报告
- 预算与实际情况分析
- 费用跟踪与分类
- 按部门/产品细分收入
-
库存管理
- 按地点和产品类别查看库存水平
- 库存流动分析与周转率
- 计算再订货点
-
商业智能
- 支持下钻的 KPI 仪表板
- 跨职能数据分析
- 高管摘要报告
-
客户分析
- 客户细分与画像
- 购买模式分析
- 留存率与流失率指标
-
运营报告
- 跟踪生产效率
- 资源利用率分析
- 质量指标与缺陷跟踪
关键属性和配置
基本属性:
ItemSource- 数据透视网格的数据源(IEnumerable、DataTable 等)PivotRows- 显示为行标题的字段集合PivotColumns- 显示为列标题的字段集合PivotCalculations- 汇总计算(Sum、Average、Count 等)ShowPivotTableFieldList- 启用/禁用架构设计器GridVisualStyles- 应用视觉主题(Metro、Office2016 等)
自定义属性:
EnableDrillDown- 允许用户展开/折叠分组数据ShowGrandTotals- 显示总计行和总计列ShowSubTotals- 显示小计行和小计列AllowSelection- 启用单元格选择AllowFiltering- 启用筛选界面AllowSorting- 启用排序功能
有关所有属性、方法和事件的详细文档,请参阅上面的参考文件。