Gridcontrol纵向/横向合并单元格

指定列值相同,纵向合并:

this.gridView1.OptionsView.AllowCellMerge = true;//启用合并列
// 启用指定合并列事件
this.gridView1.CellMerge += new DevExpress.XtraGrid.Views.Grid.CellMergeEventHandler(gridView1_CellMerge);

#region 合并指定的列
private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
{
     int rowHandle1 = e.RowHandle1;//合并列的第一行
     int rowHandle2 = e.RowHandle2;//合并列从1开始到最后行就是
     string strValue1 = this.gridView1.GetListSourceRowCellValue(rowHandle1, "age").ToString();//age:列名称
     string strValue2 = this.gridView1.GetListSourceRowCellValue(rowHandle2, "age").ToString();//age:列名称
     if (strValue1 != strValue2)
     {
         e.Merge = false; //值相同的2个单元格是否要合并在一起
         e.Handled = true; //合并单元格是否已经处理过,无需再次进行省缺处理
     }
 }
#endregion

指定列值相同,纵向合并

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] });
        }
    }
 }
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;
        }
    }
}

DEV GridControl横向合并单元格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值