syncfusion-wpf-excel-like-grid

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Implementing Syncfusion WPF GridControl

Syncfusion WPF GridControl 实现指南

The Syncfusion WPF GridControl is a high-performance, cell-oriented grid that functions as a flexible tabular display engine. It makes no assumptions about data structure, supports virtual (on-demand) data loading via
QueryCellInfo
, and provides Excel-like behaviors including formulas, import/export, clipboard operations, covered cells, and 20+ built-in cell types.
Syncfusion WPF GridControl 是一款高性能、面向单元格的表格控件,可作为灵活的表格显示引擎。它不对数据结构做任何假设,支持通过
QueryCellInfo
实现虚拟(按需)数据加载,并提供类Excel的行为,包括公式、导入/导出、剪贴板操作、合并单元格以及20余种内置单元格类型。

When to Use This Skill

何时使用本技能

  • Adding a GridControl to a WPF application (XAML or code-behind)
  • Populating cells via direct assignment or the virtual
    QueryCellInfo
    event
  • Configuring cell types (CheckBox, ComboBox, DateTimeEdit, FormulaCell, etc.)
  • Customizing cell appearance (background, font, borders, data formats)
  • Managing rows and columns (insert, remove, move, freeze, hide)
  • Clipboard, undo/redo, and editing behavior
  • Interactive features: drag-drop columns, cell drag-drop, resizing
  • Formula cell support and the built-in formula library
  • Exporting grid data to Excel; importing Excel workbooks into the grid
  • Selection modes, events, and virtual mode patterns
  • Performance tuning, virtualization, covered ranges, zooming
  • Advanced features: comments, tooltips, printing, serialization, autofit
  • 在WPF应用中添加GridControl(XAML或代码后置)
  • 通过直接赋值或虚拟
    QueryCellInfo
    事件填充单元格
  • 配置单元格类型(CheckBox、ComboBox、DateTimeEdit、FormulaCell等)
  • 自定义单元格外观(背景、字体、边框、数据格式)
  • 管理行和列(插入、删除、移动、冻结、隐藏)
  • 剪贴板、撤销/重做以及编辑行为
  • 交互功能:列拖拽、单元格拖拽、调整大小
  • 公式单元格支持及内置公式库
  • 将表格数据导出至Excel;将Excel工作簿导入至表格
  • 选择模式、事件及虚拟模式模式
  • 性能调优、虚拟化、合并区域、缩放
  • 高级功能:批注、工具提示、打印、序列化、自动调整

Documentation and Navigation Guide

文档与导航指南

Getting Started

入门指南

📄 Read: references/getting-started.md
  • Required assembly references
  • Adding GridControl via designer or programmatically
  • Setting
    RowCount
    and
    ColumnCount
  • Populating data by loop vs.
    QueryCellInfo
    virtual event
  • Minimal working example
📄 阅读: references/getting-started.md
  • 所需程序集引用
  • 通过设计器或编程方式添加GridControl
  • 设置
    RowCount
    ColumnCount
  • 通过循环或
    QueryCellInfo
    虚拟事件填充数据
  • 最简运行示例

Cell Types

单元格类型

📄 Read: references/cell-types.md
  • 20+ built-in cell types: Header, Static, CheckBox, Button, Image, ComboBox, DropDownList, DateTimeEdit, IntegerEdit, DoubleEdit, CurrencyEdit, PercentEdit, MaskEdit, UpDownEdit, RichText, DataBoundTemplate, NestedGrid (ScrollGrid)
  • Setting
    Style.CellType
    on individual cells or ranges
  • ComboBox with
    ChoiceList
    vs.
    ItemsSource
  • Custom cell types (CellModel + CellRenderer pattern)
📄 阅读: references/cell-types.md
  • 20余种内置单元格类型:Header、Static、CheckBox、Button、Image、ComboBox、 DropDownList、DateTimeEdit、IntegerEdit、DoubleEdit、CurrencyEdit、PercentEdit、 MaskEdit、UpDownEdit、RichText、DataBoundTemplate、NestedGrid (ScrollGrid)
  • 在单个单元格或区域上设置
    Style.CellType
  • ChoiceList
    ItemsSource
    的ComboBox
  • 自定义单元格类型(CellModel + CellRenderer模式)

Appearance and Styling

外观与样式

📄 Read: references/appearance-and-styling.md
  • GridStyleInfo
    class and style inheritance hierarchy
  • Volatile vs. render cell styles
  • Base styles (
    BaseStylesMap
    ), TableStyle, RowStyles, ColumnStyles
  • Background (solid/gradient), Foreground, Font, text orientation, borders
  • Numeric and DateTime format strings;
    FormatProvider
    interface
📄 阅读: references/appearance-and-styling.md
  • GridStyleInfo
    类及样式继承层次
  • 可变样式与渲染单元格样式
  • 基础样式(
    BaseStylesMap
    )、表格样式、行样式、列样式
  • 背景(纯色/渐变)、前景色、字体、文本方向、边框
  • 数值与DateTime格式字符串;
    FormatProvider
    接口

Managing Rows and Columns

行与列管理

📄 Read: references/managing-rows-and-columns.md
  • Setting row/column counts and default sizes
  • Hiding and unhiding rows/columns (
    SetHidden
    )
  • Freezing rows/columns; footer and header rows/columns
  • Inserting, moving, and removing rows/columns at runtime
  • Resize behavior and disabling resize controllers
📄 阅读: references/managing-rows-and-columns.md
  • 设置行/列数量及默认大小
  • 隐藏与取消隐藏行/列(
    SetHidden
  • 冻结行/列;页脚与页眉行/列
  • 在运行时插入、移动和删除行/列
  • 调整大小行为及禁用调整大小控制器

Editing, Clipboard, and Undo/Redo

编辑、剪贴板与撤销/重做

📄 Read: references/editing-clipboard-undo.md
  • CopyPasteOption
    flags (CopyText, CopyCellData, PasteCell, CutCell, XmlCopyPaste)
  • TextDataExchange
    for custom delimiters and buffer-based operations
  • Clipboard events and
    IGridCopyPaste
    custom implementation
  • CommandStack
    : Undo/Redo,
    BeginTrans
    ,
    CommitTrans
    ,
    Rollback
  • Creating derived commands for extended undo/redo
📄 阅读: references/editing-clipboard-undo.md
  • CopyPasteOption
    标志(CopyText、CopyCellData、PasteCell、CutCell、XmlCopyPaste)
  • TextDataExchange
    用于自定义分隔符及基于缓冲区的操作
  • 剪贴板事件与
    IGridCopyPaste
    自定义实现
  • CommandStack
    :撤销/重做、
    BeginTrans
    CommitTrans
    Rollback
  • 创建派生命令以扩展撤销/重做功能

Interactive Features

交互功能

📄 Read: references/interactive-features.md
  • Column drag-drop (
    AllowDragColumns
    )
  • Excel-like cell drag-drop (
    AllowDragDrop
    ,
    DataObjectConsumerOptions
    )
  • Runtime row/column resizing and disabling resize controllers
  • Hide/unhide rows and columns visually (
    HiddenBorderBrush
    ,
    SetHidden
    )
📄 阅读: references/interactive-features.md
  • 列拖拽(
    AllowDragColumns
  • 类Excel的单元格拖拽(
    AllowDragDrop
    DataObjectConsumerOptions
  • 运行时行/列调整大小及禁用调整大小控制器
  • 可视化隐藏/取消隐藏行和列(
    HiddenBorderBrush
    SetHidden

Formula Cells

公式单元格

📄 Read: references/formula-cells.md
  • Enabling
    FormulaCell
    type for individual cells or the entire grid
  • Built-in formula library (150+ functions: Sum, Avg, Sqrt, Pow, Cos, Sin, etc.)
  • Arithmetic operators and calculation precedence
  • Cross-cell references (A1 notation), named ranges
  • Adding custom formula functions to the library
📄 阅读: references/formula-cells.md
  • 为单个单元格或整个表格启用
    FormulaCell
    类型
  • 内置公式库(150余种函数:Sum、Avg、Sqrt、Pow、Cos、Sin等)
  • 算术运算符与计算优先级
  • 跨单元格引用(A1表示法)、命名区域
  • 向库中添加自定义公式函数

Excel Import and Export

Excel导入与导出

📄 Read: references/excel-import-export.md
  • Required assemblies (
    Syncfusion.XlsIO
    ,
    Syncfusion.GridConverter.Wpf
    )
  • Exporting entire or selected grid content to .xls / .xlsx
  • Importing Excel workbooks: styles, formulas, conditional formatting, freeze panes, hyperlinks, comments
  • Virtualized import for large workbooks
📄 阅读: references/excel-import-export.md
  • 所需程序集(
    Syncfusion.XlsIO
    Syncfusion.GridConverter.Wpf
  • 将整个或选定表格内容导出至.xls/.xlsx
  • 导入Excel工作簿:样式、公式、条件格式、冻结窗格、 超链接、批注
  • 针对大型工作簿的虚拟化导入

Selection and Events

选择与事件

📄 Read: references/selection-and-events.md
  • Selection modes: cell, row, column (
    AllowSelection
    flags)
  • QueryCellInfo
    event pattern for virtual data loading
  • PrepareRenderCell
    for view-specific overrides
  • Common grid events:
    CommitCellInfo
    ,
    CurrentCellActivated
    , clipboard events,
    RowsInserted
    ,
    ColumnsMoved
    ,
    ResizingRows
    , etc.
  • Working with
    GridRangeInfo
    and
    SelectedRanges
📄 阅读: references/selection-and-events.md
  • 选择模式:单元格、行、列(
    AllowSelection
    标志)
  • 用于虚拟数据加载的
    QueryCellInfo
    事件模式
  • 用于视图特定重写的
    PrepareRenderCell
  • 常见表格事件:
    CommitCellInfo
    CurrentCellActivated
    、剪贴板事件、
    RowsInserted
    ColumnsMoved
    ResizingRows
  • 使用
    GridRangeInfo
    SelectedRanges

Performance and Virtualization

性能与虚拟化

📄 Read: references/performance-virtualization.md
  • Virtual mode fundamentals and
    QueryCellInfo
    best practices
  • Covered ranges (
    CoveredCells
    ) and floating cells
  • Zooming the grid
  • High-frequency update patterns
📄 阅读: references/performance-virtualization.md
  • 虚拟模式基础与
    QueryCellInfo
    最佳实践
  • 合并区域(
    CoveredCells
    )与浮动单元格
  • 表格缩放
  • 高频更新模式

Advanced Features

高级功能

📄 Read: references/advanced-features.md
  • Cell comments, input-message tips, and tooltips
  • Cell layout customization (covered cells, merged ranges, autofit)
  • Printing grid content
  • Serialization: saving and loading grid state
  • Autofit rows and columns to content
📄 阅读: references/advanced-features.md
  • 单元格批注、输入消息提示与工具提示
  • 单元格布局自定义(合并单元格、合并区域、自动调整)
  • 打印表格内容
  • 序列化:保存与加载表格状态
  • 根据内容自动调整行和列

Quick Start Example

快速入门示例

csharp
   1. Add assemblies: 
      Syncfusion.Grid.Wpf
      Syncfusion.GridCommon.Wpf,
      Syncfusion.Linq.Base, 
      Syncfusion.Shared.Wpf

  // 2. XAML — place GridControl inside a ScrollViewer
<ScrollViewer>
     <syncfusion:GridControl x:Name="gridControl" />
</ScrollViewer>

// 3. Code-behind: set size and populate
gridControl.Model.RowCount = 100;
gridControl.Model.ColumnCount = 10;

// Option A — direct assignment
for (int i = 0; i < 100; i++)
    for (int j = 0; j < 10; j++)
        gridControl.Model[i, j].CellValue = $"{i}/{j}";

// Option B — virtual mode (preferred for large data)
gridControl.QueryCellInfo += (s, e) =>
{
    if (e.Cell.RowIndex > 0 && e.Cell.ColumnIndex > 0)
        e.Style.CellValue = $"{e.Cell.RowIndex}/{e.Cell.ColumnIndex}";
};
csharp
   1. Add assemblies: 
      Syncfusion.Grid.Wpf
      Syncfusion.GridCommon.Wpf,
      Syncfusion.Linq.Base, 
      Syncfusion.Shared.Wpf

  // 2. XAML — place GridControl inside a ScrollViewer
<ScrollViewer>
     <syncfusion:GridControl x:Name="gridControl" />
</ScrollViewer>

// 3. Code-behind: set size and populate
gridControl.Model.RowCount = 100;
gridControl.Model.ColumnCount = 10;

// Option A — direct assignment
for (int i = 0; i < 100; i++)
    for (int j = 0; j < 10; j++)
        gridControl.Model[i, j].CellValue = $"{i}/{j}";

// Option B — virtual mode (preferred for large data)
gridControl.QueryCellInfo += (s, e) =>
{
    if (e.Cell.RowIndex > 0 && e.Cell.ColumnIndex > 0)
        e.Style.CellValue = $"{e.Cell.RowIndex}/{e.Cell.ColumnIndex}";
};

Common Patterns

常见模式

Apply a Cell Type to a Range

为区域应用单元格类型

csharp
// Set an entire column to CheckBox
for (int i = 1; i <= gridControl.Model.RowCount; i++)
    gridControl.Model[i, 3].CellType = "CheckBox";
csharp
// Set an entire column to CheckBox
for (int i = 1; i <= gridControl.Model.RowCount; i++)
    gridControl.Model[i, 3].CellType = "CheckBox";

Alternate Row Colors via PrepareRenderCell

通过PrepareRenderCell实现交替行颜色

csharp
gridControl.PrepareRenderCell += (s, e) =>
{
    if (e.Cell.RowIndex > 0 && e.Cell.RowIndex % 2 == 0)
        e.Style.Background = Brushes.LightSkyBlue;
};
csharp
gridControl.PrepareRenderCell += (s, e) =>
{
    if (e.Cell.RowIndex > 0 && e.Cell.RowIndex % 2 == 0)
        e.Style.Background = Brushes.LightSkyBlue;
};

Freeze Header and Enable Formula Cells

冻结页眉并启用公式单元格

csharp
gridControl.Model.FrozenRows = 1;
gridControl.Model.HeaderRows = 1;
gridControl.BaseStylesMap["Standard"].StyleInfo.CellType = "FormulaCell";
csharp
gridControl.Model.FrozenRows = 1;
gridControl.Model.HeaderRows = 1;
gridControl.BaseStylesMap["Standard"].StyleInfo.CellType = "FormulaCell";

Export to Excel

导出至Excel

csharp
gridControl.Model.ExportToExcel(@"Output.xlsx", ExcelVersion.Excel2007);
csharp
gridControl.Model.ExportToExcel(@"Output.xlsx", ExcelVersion.Excel2007);

Key Style Properties

关键样式属性

PropertyPurpose
Model[r, c].CellType
Sets built-in or custom cell type string
Model[r, c].CellValue
Data value stored in the cell
Model[r, c].Background
Cell background brush
Model[r, c].Foreground
Cell text color
Model[r, c].Font.FontSize
Cell font size
Model[r, c].Borders
Individual border pens (Top/Bottom/Left/Right)
Model[r, c].Format
Format string (e.g.,
"C"
,
"0.00"
,
"d"
)
Model.RowHeights[r]
Row height in device-independent units
Model.ColumnWidths[c]
Column width
Model.FrozenRows
Number of rows frozen at top
Model.FrozenColumns
Number of columns frozen at left
undefined
PropertyPurpose
Model[r, c].CellType
设置内置或自定义单元格类型字符串
Model[r, c].CellValue
单元格中存储的数据值
Model[r, c].Background
单元格背景画刷
Model[r, c].Foreground
单元格文本颜色
Model[r, c].Font.FontSize
单元格字体大小
Model[r, c].Borders
单独的边框画笔(上/下/左/右)
Model[r, c].Format
格式字符串(例如
"C"
,
"0.00"
,
"d"
Model.RowHeights[r]
行高(设备独立单位)
Model.ColumnWidths[c]
列宽
Model.FrozenRows
顶部冻结的行数
Model.FrozenColumns
左侧冻结的列数
undefined