记 DEV bandedGrid 横向合并两列相邻的单元格

横向合并
在这里插入图片描述

  #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
做了简单处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值