参考:传送门
源码下载地址:下载
以下源码:
待合并单元格对象
using System; using System.Collections.Generic; using System.Linq; using System.Text;
using DevExpress.XtraGrid.Columns; namespace MyUtil { public class MyMergedCell { public MyMergedCell(int rowHandle1, GridColumn col1,int rowHandle2,GridColumn col2) { RowHandle1 = rowHandle1; Column1 = col1; RowHandle2 = rowHandle2; Column2 = col2; } public int RowHandle1 { get; set; } public GridColumn Column1 { get; set; } public int RowHandle2 { get; set; } public GridColumn Column2 { get; set; } } }
重绘单元格操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text;
using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.Drawing; using DevExpress.Utils.Drawing; using System.Drawing; using System.Windows.Forms; namespace MyUtil { public class MyGridPainter:GridPainter { public MyGridPainter(GridView view):base(view) { } public bool IsCustomPainting { get; set; } public void DrawMergedCell(MyMergedCell cell,PaintEventArgs e) { int delta=cell.Column1.VisibleIndex - cell.Column2.VisibleIndex; int rowdelta = cell.RowHandle1 - cell.RowHandle2; if(Math.Abs(delta)>1||Math.Abs(rowdelta)>1) { return; } GridViewInfo vi=(GridViewInfo)View.GetViewInfo(); GridCellInfo gridCellInfo1=vi.GetGridCellInfo(cell.RowHandle1, cell.Column1.AbsoluteIndex); GridCellInfo gridCellInfo2=vi.GetGridCellInfo(cell.RowHandle2, cell.Column2.AbsoluteIndex); if(gridCellInfo1==null || gridCellInfo2==null) { return; } if (gridCellInfo1.IsMerged || gridCellInfo2.IsMerged) { return; } Rectangle targetRect=Rectangle.Union(gridCellInfo1.Bounds, gridCellInfo2.Bounds); gridCellInfo1.Bounds = targetRect; gridCellInfo1.CellValueRect = targetRect; gridCellInfo2.Bounds = targetRect; gridCellInfo2.CellValueRect = targetRect; if (delta < 0||rowdelta<0) { gridCellInfo1 = gridCellInfo2; } Rectangle bounds = gridCellInfo1.ViewInfo.Bounds; bounds.Width = targetRect.Width; bounds.Height = targetRect.Height; gridCellInfo1.ViewInfo.Bounds = bounds;