概述
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());
}
}
}
最后
以上就是虚幻水杯为你收集整理的DEV GridControl横向合并单元格的全部内容,希望文章能够帮你解决DEV GridControl横向合并单元格所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复