C# 横向合并

方法1(表格线仍在)

using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System.Drawing;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using DevExpress.XtraPrinting.Export.Pdf;
namespace HorizontalMerging
{
    public class MyGridViewHandler
    {
        protected GridView view_;
        public GridView View { get { return view_; } }
        protected List<MyMergedCellInfo> mergedCells = new List<MyMergedCellInfo>();
        /*
        说明:保留值,行号,需合并的列
        using HorizontalMerging;
        MyGridViewHandler ViewHandler = null;
        ViewHandler = new MyGridViewHandler(bandedGridView1);
        ViewHandler.MergeCells(bandedGridView1.GetRowCellValue(dtNew.Rows.Count - 1, "仓库名称").ToString(), bandedGridView1.GetDataSourceRowIndex(dtNew.Rows.Count - 1), new GridColumn[] { bandedGridView1.Columns[0], bandedGridView1.Columns[1] });
        */
        public void MergeCells(string sValue, int iRowHandle, GridColumn[] gridColumns)
        {
            MyMergedCellInfo myCellInfo = new MyMergedCellInfo(sValue, iRowHandle);
            foreach (GridColumn item in gridColumns)
            {
                myCellInfo.Columns.Add(item);
            }
            mergedCells.Add(myCellInfo);
        }
        public MyGridViewHandler(GridView someView)
        {
            view_ = someView;
            view_.CustomDrawCell += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(view_CustomDrawCell);
        }
        void view_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            Rectangle textRect = e.Bounds;
            MyMergedCellInfo currentInfo = null;
            foreach (MyMergedCellInfo item in mergedCells)
            {
                if (item.RowHandle == View.GetDataSourceRowIndex(e.RowHandle) && item.Columns.Contains(e.Column))
                {
                    currentInfo = item;
                    break;
                }
            }
            if (currentInfo != null)
            {
                int clipBoundsX = 0;                
                RectangleF currentClip = e.Cache.ClipInfo.MaximumBounds;
                if (currentInfo != null)
                {
                    //获取单元格大小,单元格大小合并
                    foreach (GridColumn item in currentInfo.Columns)
                    {
                        if (item == e.Column) continue;
                        if (currentInfo.Columns.IndexOf(item) > currentInfo.Columns.IndexOf(e.Column))
                        {
                            textRect.Width += item.VisibleWidth;
                        }
                        else
                        {
                            textRect.X -= item.VisibleWidth;
                            textRect.Width += item.VisibleWidth;
                        }
                    }
                    //传入保留的值
                    e.DisplayText = currentInfo.DisplayText;
                    clipBoundsX = (int)currentClip.X < e.Bounds.X ? e.Bounds.X - 4 : (int)currentClip.X;
                    if (View.LeftCoord > 0)
                        e.Cache.ClipInfo.SetClip(new Rectangle(clipBoundsX, (int)currentClip.Y, textRect.Width, (int)currentClip.Height));
                    IndentInfoCollection lines = (e.Cell as GridCellInfo).RowInfo.Lines;
                    List<IndentInfo> removedLines = new List<IndentInfo>();
                    foreach (IndentInfo currentLine in lines) {
                        if (textRect.X <= (currentLine.Bounds.X - View.LeftCoord) && (textRect.Width + textRect.X) >= (currentLine.Bounds.X - View.LeftCoord) && currentLine.Bounds.Y <= textRect.Y && (currentLine.Bounds.Y + currentLine.Bounds.Height) >= textRect.Y) {
                            currentLine.OffsetContent(-currentLine.Bounds.X, -currentLine.Bounds.Y); }
			        }
                }
                //配置外观
                e.Appearance.DrawBackground(e.Graphics, e.Cache, textRect);
                e.Appearance.DrawString(e.Cache, e.DisplayText, textRect);
                e.Handled = true;
                e.Cache.ClipInfo.SetClip(new Rectangle((int)currentClip.X, (int)currentClip.Y, (int)currentClip.Width, (int)currentClip.Height));
            }
        }
    }
    public class MyMergedCellInfo
    {
        List<GridColumn> columns_;
        string displayText_;
        int rowHandle_;
        public List<GridColumn> Columns
        {
            get { return columns_; }
        }
        public string DisplayText
        {
            get { return displayText_; }
        }
        public int RowHandle
        {
            get { return rowHandle_; }
        }
        //保留值,行号
        public MyMergedCellInfo(string sDisplayText, int iRowHandle)
        {
            columns_ = new List<GridColumn>();
            displayText_ = sDisplayText;
            rowHandle_ = iRowHandle;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑先生Ethan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值