<c1:C1FlexGrid Name="CfgRiskReviewItem" Style="{StaticResource Green}" ItemsSource="{Binding RiskReviewList,Mode=TwoWay}" SelectedItem="{Binding CurrentRiskReviewItem,Mode=TwoWay}"
LoadedRows="FlexGrid_LoadedRiskReviewRows_RowIndex" PreviewKeyDown="c1FlexGrid_PreviewKeyDown" MinRowHeight="50" Margin="5,5,5,0" AllowMerging="Cells"
表格增加AllowMerging="Cells"属性
cs代码初始化方法里面写上
CfgRiskReviewItem.CellFactory = new FlexGridShowCellFactory();
CfgRiskReviewItem.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 "RiskName":
return SetRg(grid, rg, new List<string>() { colName });
// 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();
}
}
效果