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

}

}

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

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

相关推荐
one99611 小时前
WPF-绑定
microsoft·c#·.net·wpf
one99618 小时前
WPF 数据绑定中的通知机制及其性能考虑
c#·.net·wpf
△曉風殘月〆1 天前
WPF自定义任务栏缩略图
wpf·dwm·任务栏
weixin_464078072 天前
WPF使用ContentControl控件实现区域导航,并使用Prism依赖注入优化
wpf
桂月二二2 天前
深入解析Reactive Programming框架在微服务中的最新应用
微服务·架构·wpf
军训猫猫头2 天前
31.九个按钮排列 C#例子 WPF例子
ui·c#·wpf
桂月二二3 天前
探讨面向未来的框架新技术:逻辑驱动和自适应框架的突破
wpf
△曉風殘月〆3 天前
WPF中的Microsoft XAML Behaviors包功能详解
wpf·xaml·behavior
喜欢猪猪4 天前
Apollo中间件技术:从入门到精通
wpf
Thinbug4 天前
WFP Listbox绑定数据后,数据变化的刷新
wpf·listbox