datagridview 纵向 横向 合并单元格
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 纵向 横向 合并单元格的更多相关文章
- Windows Forms DataGridView中合并单元格
Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.D ...
- python-Excel读取-合并单元格读取
python-Excel读取-合并单元格读取(后续会补充python-Excel写入的部分) 1. python读取Excel单元格 代码包含读取Excel中数据,以及出现横向合并单元格,以及竖向合并 ...
- 【Javascript】Javascript横向/纵向合并单元格TD
> 需求是这样滴(>_<) 在报表系统中,涉及“HTML的TD单元格的合并”恐怕为数不少. 比如,从DB查得数据并经过后台的整理后,可能是这样的: Table1 JOB TO ...
- DataGridView合并单元格(多行多列合并)
一.点击在拖入的显示控件(TreeList)右上方的箭头,在Treelist任务中选择数据源,添加项目数据源,依次选择数据库.数据集,新建连接,浏览选择数据库(*.mdb),依次点击 下一步,选择“表 ...
- DataGridView合并单元格(一列或一行)
#region"合并单元格的测试(一列或一行)" // int?是搜索一种类型(可空类型),普通的int不能为null,而用int?,其值可以为null //private int ...
- DataGridView合并单元格
昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,然后直接使用e.Graphics.FillRectangle(backColorBru ...
- dev GridControl直接打印 纵向合并单元格
GridControl纵向合并单元格 只需设置 gridView->OptionView->AllowCellMerge=true; 效果 提示: 精确到列 前提是gridview1已经允 ...
- RDLC报表纵向合并单元格。
在做RDLC报表时发现居然没有纵向合并单元格,震惊! 网上查了一些资料,有些方法很可爱,采用去除边框法,但是用这种方法如果要求文本属性居中的话那则达不到美观效果,还有些复杂一点的方法,我都没耐心看,然 ...
- JS实现EasyUI ,Datagrid,合并单元格功能
为了实现datagrid的合并单元格效果,datagrid的数据加载方式肯定是要写在JS文件内部的. 一:在JS内部添加Datagrid数据加载方法如下: $("#id").dat ...
随机推荐
- GUC-4 CopyOnWriteArrayList/CopyOnWriteArraySet
/* * CopyOnWriteArrayList/CopyOnWriteArraySet : “写入并复制” * 注意:添加操作多时,效率低,因为每次添加时都会进行复制,开销非常的大.并发迭代操作多 ...
- 4款Github泄漏敏感信息搜索工具简单比较
gitrob Ruby开发,支持通过postgresql数据库https://github.com/michenriksen/gitrob weakfilescan Python开发,多线程,猪猪侠开 ...
- Python类总结-封装(私有属性,方法)
封装基础 广义上面向对象的封装:代码的保护,面向对象的思想本身就是一种封装 只让自己的对象能调用自己类中的方法 狭义上的封装-面向对象三大特性之一(私有变量,用公有的方法封装私有属性,方法叫封装) 把 ...
- React Native 系列(三)
前言 本系列是基于React Native版本号0.44.3写的,相信大家看了本系列前面两篇文章之后,对于React Native的代码应该能看懂一点点了吧.本篇文章将带着大家来认识一下React N ...
- React Native 系列(四)
前言 本系列是基于React Native版本号0.44.3写的.RN支持CSS中的布局属性,因此可以使用CSS布局属性,这里就不详细地讲解了,这篇文章的重点主要是讲述一下RN中的Flex布局. CS ...
- 「2017 山东一轮集训 Day4」基因
设置 \(\sqrt{n}\) 个关键点,维护出关键点到每个右端点之间的答案以及Pam的左指针,每次暴力向左插入元素即可,为了去重,还需要记录一下Pam上每个节点在每个关键点为左端点插入到时候到最左边 ...
- 是否排序好了 Exercise07_19
import java.util.Scanner; import java.util.Arrays; /** * @author 冰樱梦 * 时间2018年12月 * 题目:是否排序好了 * */ p ...
- 【manacher】模板
考试竟然写错了manacher!太耻辱了!所以赶快又敲了一遍模板!!一定不能错了aaaa #include<iostream> #include<cstdio> #includ ...
- bzoj 1045
确定初始状态(n与1直接谁给了谁几个),后面的就确定了,再根据总结出来的东西决定前面谁给谁几个最优. n=1000000!!! /*********************************** ...
- Linux下对拍程序
在程序对应文件夹下存为.sh文件 在终端命令中进入相应文件夹,用 sh XXX.sh 调用 while true; do ./datamaker>tmp.in ./baoli<tmp.in ...