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

}

}

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

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

相关推荐
"孙小浩14 小时前
HarmonyOS应用开发者高级-编程题-001
华为·wpf·harmonyos
yngsqq14 小时前
003集——《利用 C# 与 AutoCAD API 开发 WPF 随机圆生成插件》(侧栏菜单+WPF窗体和控件+MVVM)
wpf
baivfhpwxf20231 天前
WPF 免费UI 控件HandyControl
ui·wpf
qq_196055871 天前
WPF插入背景图
wpf
baivfhpwxf20231 天前
prism WPF 对话框
c#·wpf
baivfhpwxf20232 天前
WPF 登录页面
ui·wpf
baivfhpwxf20232 天前
prism WPF 模块
wpf
baivfhpwxf20232 天前
prism WPF 导航
wpf
军训猫猫头2 天前
87.在线程中优雅处理TryCatch返回 C#例子 WPF例子
开发语言·ui·c#·wpf
huizhixue-IT3 天前
华为存储考试内容&HCIP-Storage
wpf