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

}

}

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

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

相关推荐
行走正道2 小时前
【探索实战】跨云应用分发自动化实战:基于Kurator的统一交付体系深度解析
运维·自动化·wpf·kurator·跨云分发
Macbethad6 小时前
基于WPF的Ethernet/IP主站程序技术方案
网络协议·tcp/ip·wpf
张人玉10 小时前
Prism Template Pack 完整使用示例(VS2022 + .NET 8 + DryIoc)
.net·wpf·prism
棉晗榜11 小时前
wpf 在XAML中配置视图模型,通过 d:DataContext设置设计时类型,方便按F12跳转查看类型
wpf
赵财猫._.14 小时前
HarmonyOS渲染性能优化:组件树复用与局部刷新机制
wpf·harmonyos·ux
赵财猫._.14 小时前
鸿蒙分布式数据库同步:冲突解决与数据一致性策略
wpf·harmonyos·ux
Macbethad1 天前
使用WPF编写一个数据记录页面
wpf
dotent·3 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图4 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞4 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native