datagridview 单元格合并:纵向以及横向合并参考了csdn上不知哪位的代码,具体哪位找不到连接了。

纵向合并:

        /// <summary>
/// 纵向合并,即合并数据项的值
/// </summary>
/// <param name="e"></param>
private void DrawCellVer(DataGridViewCellPaintingEventArgs e)
{
if (e.CellStyle.Alignment != DataGridViewContentAlignment.MiddleCenter)
{
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
Brush gridBrush = new SolidBrush(this.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
int cellwidth;
//上面相同的行数
int UpRows = 0;
//下面相同的行数
int DownRows = 0;
//总行数
int count = 0;
if (true)
{
cellwidth = e.CellBounds.Width;
Pen gridLinePen = new Pen(gridBrush);
//获取当前单元格的值,如果为null,就赋值为""
string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
if (!string.IsNullOrEmpty(curValue))
{
#region 获取下面的行数
for (int i = e.RowIndex; i < this.Rows.Count; i++)
{
if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
DownRows++;
if (e.RowIndex != i)
{
cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
}
}
else
break;
}
#endregion #region 获取上面的行数
for (int i = e.RowIndex; i >= 0; i--)
{
if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
UpRows++;
if (e.RowIndex != i)
{
cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
}
}
else
break;
}
#endregion
count = DownRows + UpRows - 1; if (count < 2)
{
return;
}
}
else
{
//取下面看是否有为空的单元格,如果为最后一个为空的单元格,则画下边线
if (e.RowIndex < this.Rows.Count - 1)
{
if (this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != DBNull.Value)
{
if (!string.IsNullOrEmpty(this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString()))
{
DownRows = 1;
} }
}
}
if (this.Rows[e.RowIndex].Selected)
{
backBrush.Color = e.CellStyle.SelectionBackColor;
//fontBrush.Color = e.CellStyle.SelectionForeColor;
}
//以背景色填充
e.Graphics.FillRectangle(backBrush, e.CellBounds);
//画字符串
PaintingFont(e, cellwidth, UpRows, DownRows, count);
if (DownRows == 1)
{
//画下面的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
count = 0;
}
// 画右边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); e.Handled = true;
}
}

  横向合并:

        /// <summary>
/// 水平合并单元格,即数据头的合并
/// </summary>
/// <param name="e"></param>
private void DrawCellHor(DataGridViewCellPaintingEventArgs e)
{
if (e.CellStyle.Alignment != DataGridViewContentAlignment.MiddleCenter)
{
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
Brush gridBrush = new SolidBrush(this.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
int cellwidth;
//前面相同的行数
int BefRows = 0;
//后面相同的行数
int BehRows = 0;
//总列数
int count = 0;
if (true)
{
cellwidth = e.CellBounds.Width;
Pen gridLinePen = new Pen(gridBrush);
//获取当前单元格的值,如果为null,就赋值为""
string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
if (!string.IsNullOrEmpty(curValue))
{
#region 获取后面的列数
for (int i = e.ColumnIndex; i < this.ColumnCount; i++)
{
if (this.Rows[e.RowIndex].Cells[i].Value.ToString().Equals(curValue))
{
BehRows++;
if (e.ColumnIndex != i)
{
cellwidth = cellwidth < this.Rows[e.RowIndex].Cells[i].Size.Width ? cellwidth : this.Rows[e.RowIndex].Cells[i].Size.Width;
}
}
else
break;
}
#endregion #region 获取前面的列数
for (int i = e.ColumnIndex; i >= 0; i--)
{
if (this.Rows[e.RowIndex].Cells[i].Value.ToString().Equals(curValue))
{
BefRows++;
if (e.ColumnIndex != i)
{
cellwidth = cellwidth < this.Rows[e.RowIndex].Cells[i].Size.Width ? cellwidth : this.Rows[e.RowIndex].Cells[i].Size.Width;
}
}
else
break;
}
#endregion
count = BehRows + BefRows - 1; if (count < 2)
{
return;
}
}
else
{
//取右边看是否有为空的单元格,如果为最后一个为空的单元格,则画右侧线
if (e.ColumnIndex < this.ColumnCount - 1)
{
if (this.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value != DBNull.Value)
{
if (!string.IsNullOrEmpty(this.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value.ToString()))
{
BehRows = 1;
} }
}
}
if (this.Rows[e.RowIndex].Selected)
{
backBrush.Color = e.CellStyle.SelectionBackColor;
//fontBrush.Color = e.CellStyle.SelectionForeColor;
}
//以背景色填充
e.Graphics.FillRectangle(backBrush, e.CellBounds);
//画字符串
HorPaintingFont(e, cellwidth, BefRows, BehRows, count);
if (BehRows == 1)
{
//画右边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
count = 0;
}
// 画下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); e.Handled = true;
}
}

 单元格写值纵向:

        /// <summary>
/// 绘制合并以后的值(纵向)
/// </summary>
/// <param name="e"></param>
/// <param name="cellwidth"></param>
/// <param name="UpRows"></param>
/// <param name="DownRows"></param>
/// <param name="count"></param>
private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
{
if (e.Value != DBNull.Value)
{
stValue = e.Value.ToString();
}
using (SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor))
{
fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
int cellheight = e.CellBounds.Height; if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
{ e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
}
else
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
}
}

  单元格写值横向:

        /// <summary>
/// 水平方向写值
/// </summary>
/// <param name="e"></param>
/// <param name="cellwidth"></param>
/// <param name="UpRows"></param>
/// <param name="DownRows"></param>
/// <param name="count"></param>
private void HorPaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
{
using (SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor))
{
fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
int cellHeight = e.CellBounds.Height; if (e.Value != DBNull.Value)
{
stValue = e.Value.ToString();
}
if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X - cellwidth * (UpRows - 1) + (cellwidth * count - fontwidth) / 2, e.CellBounds.Y + (cellHeight - fontheight) / 2);
}
}
}

  在次备忘一下。

datagridview 纵向 横向 合并单元格的更多相关文章

  1. Windows Forms DataGridView中合并单元格

    Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.D ...

  2. python-Excel读取-合并单元格读取

    python-Excel读取-合并单元格读取(后续会补充python-Excel写入的部分) 1. python读取Excel单元格 代码包含读取Excel中数据,以及出现横向合并单元格,以及竖向合并 ...

  3. 【Javascript】Javascript横向/纵向合并单元格TD

    > 需求是这样滴(>_<) 在报表系统中,涉及“HTML的TD单元格的合并”恐怕为数不少. 比如,从DB查得数据并经过后台的整理后,可能是这样的: Table1     JOB TO ...

  4. DataGridView合并单元格(多行多列合并)

    一.点击在拖入的显示控件(TreeList)右上方的箭头,在Treelist任务中选择数据源,添加项目数据源,依次选择数据库.数据集,新建连接,浏览选择数据库(*.mdb),依次点击 下一步,选择“表 ...

  5. DataGridView合并单元格(一列或一行)

    #region"合并单元格的测试(一列或一行)" // int?是搜索一种类型(可空类型),普通的int不能为null,而用int?,其值可以为null //private int ...

  6. DataGridView合并单元格

    昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,然后直接使用e.Graphics.FillRectangle(backColorBru ...

  7. dev GridControl直接打印 纵向合并单元格

    GridControl纵向合并单元格 只需设置 gridView->OptionView->AllowCellMerge=true; 效果 提示: 精确到列 前提是gridview1已经允 ...

  8. RDLC报表纵向合并单元格。

    在做RDLC报表时发现居然没有纵向合并单元格,震惊! 网上查了一些资料,有些方法很可爱,采用去除边框法,但是用这种方法如果要求文本属性居中的话那则达不到美观效果,还有些复杂一点的方法,我都没耐心看,然 ...

  9. JS实现EasyUI ,Datagrid,合并单元格功能

    为了实现datagrid的合并单元格效果,datagrid的数据加载方式肯定是要写在JS文件内部的. 一:在JS内部添加Datagrid数据加载方法如下: $("#id").dat ...

随机推荐

  1. php极速后台开发框架LotusAdmin

    组件:基于thinkphp5.0.12+layui2.1版本 演示站点:https://www.lotusadmin.top/账号 : admin密码:123456 官方QQ交流群:606645328 ...

  2. [实战]MVC5+EF6+MySql企业网盘实战(21)——网盘操作日志

    写在前面 上篇文章介绍了一个bootstrap的分页插件,这篇将弄一个完整的例子,就以日志分页为例说明如何请求服务端然后进行分页. 系列文章 [EF]vs15+ef6+mysql code first ...

  3. [实战]MVC5+EF6+MySql企业网盘实战(5)——登录界面,头像等比例压缩

    写在前面 关于该项目,已经很久没更新了.实在是找不到一个好的ui,没办法就在网上找了一个还不错的,就凑合着先用着吧,先出第一版,以后的再想着去优化.最近更新与网盘项目相关的内容是准备在项目中使用一个美 ...

  4. <<Javascript Patterns>>阅读笔记 -- 第2章 基本技巧(一)

    第一次写这种东西, 有些生涩和蹩脚, 也是为了自己在表达或是总结方面有所提高, 同时为看过的东西留个痕迹, 以便日后查阅. 有错误或是不妥的地方, 还望各位指正, 谢谢! 第1章 简介 本章主要介绍了 ...

  5. 移动端禁止图片长按和vivo手机点击img标签放大图片,禁止长按识别二维码或保存图片【转载】

    移动端禁止图片长按和vivo手机点击img标签放大图片,禁止长按识别二维码或保存图片 img{ pointer-events: none; } 源文地址:https://www.cnblogs.com ...

  6. LR参数组取值操作方法

    LR参数组取值操作方法 奶奶的,每次都要重写一次,粘这里,以后备用.这个使用频率高. Action(){//定义一个变量int rNum; //关联参数组,前提是请求的页面中,存在参数组web_reg ...

  7. 三 Python解释器

    当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...

  8. TestDirector其他

    一.查看连接TD的用户 1.用单击界面上的“Connections”按钮,进入如下图: 二.Licenses管理 1.单击“Licenses”按钮,显示如下图: 2.单击“Modify License ...

  9. Windows环境上装在VM,VM安装CentOS7

    1.下载VM并且安装 VM下载地址:https://www.vmware.com/products/workstation-pro.html 来自百度经验的的一个密钥(VMware Workstati ...

  10. windows 10 下安装python 2.7

    下载msi的安装包: https://www.python.org/ftp/python/2.7.8/python-2.7.8.msi [incorrect] PS C:\Python27> . ...