WPF的C1FlexGrid控件合并相同列

<c1:C1FlexGrid Name="CfgRiskReviewItem" Style="{StaticResource Green}" ItemsSource="{Binding RiskReviewList,Mode=TwoWay}" SelectedItem="{Binding CurrentRiskReviewItem,Mode=TwoWay}"

LoadedRows="FlexGrid_LoadedRiskReviewRows_RowIndex" PreviewKeyDown="c1FlexGrid_PreviewKeyDown" MinRowHeight="50" Margin="5,5,5,0" AllowMerging="Cells"

表格增加AllowMerging="Cells"属性

cs代码初始化方法里面写上

CfgRiskReviewItem.CellFactory = new FlexGridShowCellFactory();

CfgRiskReviewItem.MergeManager = new SummaryProjectMergeManager();

private class SummaryProjectMergeManager : IMergeManager

{

/// <summary>

/// 获取合并块

/// </summary>

/// <param name="grid"></param>

/// <param name="cellType"></param>

/// <param name="rg"></param>

/// <returns></returns>

public CellRange GetMergedRange(C1FlexGrid grid, CellType cellType, CellRange rg)

{

var colName = grid.Columns[rg.Column].ColumnName;

if (cellType != CellType.Cell)

{

return rg;

}

switch (colName)

{

case "RiskName":

return SetRg(grid, rg, new List<string>() { colName });

// case "item2":

// return SetGeCg(grid, rg, new List<string>() { colName });

default:

return rg;

}

}

/// <summary>

/// 设置合并块

/// </summary>

/// <param name="grid"></param>

/// <param name="rg"></param>

/// <param name="depColList">合并依据列</param>

/// <returns></returns>

private CellRange SetRg(C1FlexGrid grid, CellRange rg, List<string> depColList)

{

for (int i = rg.Row; i < grid.Rows.Count - 1; i++)

{

bool isSame = true;

foreach (var depCol in depColList)

{

if (GetDataDisplay(grid, i, grid.Columns[depCol].Index) != GetDataDisplay(grid, i + 1, grid.Columns[depCol].Index))

{

isSame = false;

break;

}

}

if (!isSame)

{

break;

}

rg.Row2 = i + 1;

}

for (int i = rg.Row; i > 0; i--)

{

bool isSame = true;

foreach (var depCol in depColList)

{

if (GetDataDisplay(grid, i, grid.Columns[depCol].Index) != GetDataDisplay(grid, i - 1, grid.Columns[depCol].Index))

{

isSame = false;

break;

}

}

if (!isSame)

{

break;

}

rg.Row = i - 1;

}

return rg;

}

private CellRange SetGeCg(C1FlexGrid grid, CellRange rg, List<string> depColList)

{

for (int i = rg.Row; i < grid.Rows.Count - 1; i++)

{

if (i == 6)

{

grid[6, 6] = null;

grid[6, 7] = null;

grid[6, 8] = null;

grid.Rows[i].AllowMerging = true;

break;

}

}

return rg;

}

/// <summary>

/// 获取单元格显示值

/// </summary>

/// <param name="grid"></param>

/// <param name="r"></param>

/// <param name="c"></param>

/// <returns></returns>

private string GetDataDisplay(C1FlexGrid grid, int r, int c)

{

return (grid[r, c] ?? "").ToString();

}

}

效果

相关推荐
麻花20137 小时前
WPF表格控件的列利用模块适配动态枚举类
r语言·word·wpf
mingupup9 小时前
TesseractOCR-GUI:基于WPF/C#构建TesseractOCR简单易用的用户界面
ui·c#·wpf
He BianGu1 天前
笔记:在WPF中InvalidateMeasure,InvalidateArrange,InvalidateVisual,UpdateLayout主要功能
笔记·wpf
白露与泡影1 天前
SkyWalking 和 ELK 链路追踪实战
elk·wpf·skywalking
不爱吃米饭_1 天前
SkyWalking 和 ELK 链路追踪实战
elk·wpf·skywalking
延卿1 天前
wpf DataGrid好看的样式
ui·wpf
SEO-狼术1 天前
Telerik UI for WPF 2024 Q4 Crack
ui·wpf
mingupup2 天前
WPF Prism ViewDiscovery
wpf
为风而战2 天前
WPF 本地生成验证码
ui·wpf