当前位置: 首页 > news >正文

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" });elsetbl.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横向合并单元格

http://www.lryc.cn/news/420533.html

相关文章:

  • 从周杰伦的《青花瓷》三次更名看方文山的国学情怀与工匠精神
  • HATS:分层图注意力神经网络用于股票预测
  • 【日常记录-MySQL】MySQL设置root用户密码
  • 高级Web安全技术(第二篇)
  • 前端实现文件下载常用几种方式
  • Isaac Lab 安装 (ubuntu22.04环境)
  • todoList清单(HTML+CSS+JavaScript)
  • LVS集群实现四层负载均衡详解(以nat,dr模式为例)
  • 七夕表白网页效果实现与解析
  • 人工智能算法工程师(高级)课程11-自然语言处理之NLP的语言模型-seq2seq模型,seq+注意力与代码详解
  • 从PyTorch官方的一篇教程说开去(6.2 - 张量 tensor 矩阵运算等)
  • 【网络层】直连路由、静态路由、动态路由
  • tkinter用法总结
  • iOS基础-Block
  • 本地图片瀑布流浏览器asonry Image Viewer
  • macos重装系统 启动U盘制作方法 - createinstallmedia 命令使用方法总结
  • 八问八答搞懂Transformer内部运作原理
  • MySQL增删改查(基础)
  • Cairo库移植到安卓记录
  • Redis 哈希类型的常用命令总结
  • 【物联网设备端开发】ESP开发工具:QEMU如何模拟以太网口接入网络
  • Python学习笔记(四)
  • 跨域:安全分步实施指南
  • 【iOS】AutoreleasePool自动释放池的实现原理
  • stm32—GPIO
  • CocosCreator使用 ProtoBuf WebSocket与服务器对接方法
  • 【python基础】while循环语句练习
  • 【SpringBoot系列】WebMvcConfigurer配置
  • 学懂C++ (十九):高级教程——深入详解C++信号处理
  • SOMEIP_ETS_032:echoUINT8ArrayMinSize