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

}

}

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

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

相关推荐
没有bug.的程序员4 小时前
服务治理与 API 网关:微服务流量管理的艺术
java·分布式·微服务·架构·wpf
Brianna Home9 小时前
【案例实战】鸿蒙分布式调度:跨设备协同实战
华为·wpf·harmonyos
c#上位机1 天前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
△曉風殘月〆1 天前
如何在WPF中实现ComboBox多选
wpf
csdn_aspnet1 天前
如何使用现有工具进行 .NET 8 迁移 Wpf
wpf·.net 8
123梦野2 天前
WPF——动画
wpf
攻城狮CSU2 天前
WPF 疑点汇总2.HorizontalAlignment和 HorizontalContentAlignment
wpf
c#上位机2 天前
wpf之命令
c#·wpf
没有bug.的程序员2 天前
分布式链路追踪:微服务可观测性的核心支柱
java·分布式·微服务·架构·wpf
Aevget3 天前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(一)
c#·wpf·界面控件·devexpress·ui开发