DEV GridControl横向合并单元格

Form1.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;

namespace HorizontalMerging
{
    public partial class Form1 : Form
    {
        MyGridViewHandler ViewHandler = null;
        public Form1()
        {
            InitializeComponent();
            ViewHandler = new MyGridViewHandler(gridView1);
        }

        private DataTable CreateTable(int RowCount)
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("Name1", typeof(string));
            tbl.Columns.Add("Name2", typeof(string));
            tbl.Columns.Add("Name3", typeof(string));
            tbl.Columns.Add("Name4", typeof(string));
            tbl.Columns.Add("Name5", typeof(string));
            for (int i = 0; i < RowCount; i++)
            {
                if (i == 1)
                    tbl.Rows.Add(new object[] { String.Format("Name{0}", i), "This is a long long string, which is merged for several columns", "", "", "" });
                else if (i == 3)
                    tbl.Rows.Add(new object[] { String.Format("Name{0}", i), "This is a long long string, which is merged for several columns", "", "", "Text" });
                else
                    tbl.Rows.Add(new object[] { String.Format("Name{0}", i), String.Format("Name{0}", i), String.Format("Name{0}", i), String.Format("Name{0}", i) });
            }
            return tbl;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = CreateTable(20);
            gridView1.Columns[0].Fixed = FixedStyle.Left;
            gridView1.Columns[4].Width = 300;
            gridControl1.ForceInitialize();
            ViewHandler.MergeCells(gridView1.GetRowCellValue(1, "Name2").ToString(), gridView1.GetDataSourceRowIndex(1), new GridColumn[] { gridView1.Columns[2], gridView1.Columns[3], gridView1.Columns[4] });
            ViewHandler.MergeCells(gridView1.GetRowCellValue(3, "Name2").ToString(), gridView1.GetDataSourceRowIndex(3), new GridColumn[] { gridView1.Columns[2], gridView1.Columns[3] });
        }
    }
 }

MyGridViewHandler.cs

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;

namespace HorizontalMerging
{
    public class MyGridViewHandler
    {
        protected GridView view_;
        public GridView View { get { return view_; } }

        protected List<MyMergedCellInfo> mergedCells = new List<MyMergedCellInfo>();

        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.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;
        }
    }
}

Program.cs

using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HorizontalMerging
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值