横向合并
#region 合并合计行的单元格
private void bandedGridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.RowHandle < 0) return;
if (this.bandedGridView1.GetRowCellValue(e.RowHandle, "列A FieldName ").ToString().Contains("合计"))//找到合计行
{
//列A:this.bandedGridView1.Columns[0] 下标换成你自己对应的列
//列B:this.bandedGridView1.Columns[1]
//限定只有这两列执行合并
if (e.Column.FieldName == "列A FieldName" || e.Column.FieldName == "列B FieldName")
{
GridViewInfo vi = (GridViewInfo)this.bandedGridView1.GetViewInfo();
GridCellInfo ci1 = vi.GetGridCellInfo(e.RowHandle, this.bandedGridView1.Columns[0]); //列A
GridCellInfo ci2 = vi.GetGridCellInfo(e.RowHandle, this.bandedGridView1.Columns[1]); //列B
//!!!下面的不动就行!!!
Rectangle newR = Rectangle.Union(ci1.Bounds, ci2.Bounds); //合并两个单元格
//取消合并区域中每个单元格的边框
IndentInfoCollection lines = (e.Cell as GridCellInfo).RowInfo.Lines; //该单元格所在的行中的所有边框线
foreach (IndentInfo currentLine in lines)
{
if (newR.X <= currentLine.Bounds.X && currentLine.Bounds.X < newR.X + newR.Width && newR.Y <= currentLine.Bounds.Y && currentLine.Bounds.Y < newR.Y + newR.Height)
{
currentLine.OffsetContent(-currentLine.Bounds.X, -currentLine.Bounds.Y); //直接偏移到表格边界(即这个边框线不显示了)
}
}
string text = this.bandedGridView1.GetRowCellValue(e.RowHandle, this.bandedGridView1.Columns[0]).ToString(); //起始单元格的值
e.Appearance.DrawBackground(e.Cache, newR); //绘制背景区域
e.DisplayText = text; //当前单元格显示的文本显示为第一个起始单元格的文本
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //合并区域文本居中
e.Appearance.DrawString(e.Cache, e.DisplayText, newR); //绘制文本
e.Handled = true;
}
}
}
#endregion
来自 @星遇星雨 的文章:https://blog.youkuaiyun.com/weixin_43829957/article/details/139175996
做了简单处理