如你看到的,正是由于单元格被合并了,所以你给被合并的单元格或者对应行设置样式是没有用的,比如你截图中标注的红线,实际上都是第一行的单元格,分别应该是(1,1)(1,4)单元格,你得给这些单元格设置样式才行,按照这个思路,代码应该这样写
- try
- {
- string prevGroupValue = "";
- for (int row = grid.Rows.Fixed; row < grid.Rows.Count; row++)//Fixed=1
- {
- string currentGroupValue = grid.GetData(row, columnIndex)?.ToString() ?? ""; //获取当前行的值
- if (currentGroupValue != prevGroupValue)
- {
- if (row == grid.Rows.Fixed) //行头(列标题行)不作判断
- {
- prevGroupValue = currentGroupValue;
- continue;
- }
- // 应用分组线样式到整行,为上一行设置线
- grid.Rows[row - 1].Style = grid.Styles["GroupLine"];
- grid.SetCellStyle(row, 1, grid.Styles["GroupLine"]);
- grid.SetCellStyle(row, 4, grid.Styles["GroupLine"]);
- prevGroupValue = currentGroupValue;
- }
- else
- {
- grid.Rows[row].Style = grid.Styles.Normal; //非分组行设置为标准样式
- }
- }
- grid.SetCellStyle(1, 1, grid.Styles["GroupLine"]);
- grid.SetCellStyle(1, 4, grid.Styles["GroupLine"]);
- }
复制代码
最终效果
|