WPF合并C1FlexGrid表格,根据多列的值进行合并

在cs文件种写入

public PcbScrapListUC()

{

InitializeComponent();

VmType = typeof(PcbScrapListVM);

//合并

PcbScrapFlexGrid.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 "CustomerCodes":

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

case "ScrapPcsCount":

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

case "ProductNumberVer":

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

case "RiskName":

// 根据哪些列合并,这里是所有列都需要加上ProductNumberVer colName, "ProductNumberVer"

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

case "TopRate":

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

case "StockInPcsAreaNet":

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

case "StockInPcsCount":

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

case "PcbTypeName":

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

case "ProductNumberTypeName":

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

case "ScrapAreaNetCount":

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

case "ScrapRate":

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

// 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();

}

}

效果:所有列都根据本厂编号加单前列合并

如果只是合并当前一列,就无需传多个字段

相关推荐
code bean1 天前
【WPF】WPF 自定义控件之依赖属性
wpf
上元星如雨1 天前
WPF 加载和显示 GIF 图片的完整指南
wpf
微小冷2 天前
WPF中ListView控件详解
c#·wpf·数据绑定·listview·bingding
专注VB编程开发20年3 天前
WPF,Winform,HTML5网页,哪个UI开发速度最快?
大数据·c#·wpf
YANQ6623 天前
3.1 WPF画折线图、直方图、饼状图
wpf
上元星如雨3 天前
WPF demo:全屏加载界面
wpf
深漂阿碉3 天前
WPF自定义日历选择控件
wpf
六点的晨曦3 天前
WPF的三轴机械手控件动画
wpf
军训猫猫头4 天前
5.浏览本地文件获取路径与文件名称 C#例子 WPF例子
开发语言·c#·wpf
步、步、为营4 天前
.NET + WPF框架开发聊天、网盘、信息发布、视频播放功能
.net·wpf·音视频