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 ...
随机推荐
- MySQL5.7 centos7.2 yum 安装
1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/ # 下载mysql源安装包 shell> wge ...
- 【58沈剑架构系列】主从DB与cache一致性
本文主要讨论这么几个问题: (1)数据库主从延时为何会导致缓存数据不一致 (2)优化思路与方案 一.需求缘起 上一篇<缓存架构设计细节二三事>中有一个小优化点,在只有主库时,通过“串行化” ...
- Java 中的静态嵌套类和非静态嵌套类
Java 中的静态嵌套类和非静态嵌套类 术语:嵌套类分为两类:静态嵌套类和非静态嵌套类.声明 static 的嵌套类称为静态嵌套类,非静态嵌套类也称为内部类. class OuterClass { p ...
- 牛客网 牛客练习赛43 F.Tachibana Kanade Loves Game-容斥(二进制枚举)+读入挂
链接:https://ac.nowcoder.com/acm/contest/548/F来源:牛客网 Tachibana Kanade Loves Game 时间限制:C/C++ 1秒,其他语言2秒 ...
- 【JAVAWEB学习笔记】19_事务概述、操作、特性和隔离级别
事务 学习目标 案例-完成转账 一.事务概述 1.什么是事务 一件事情有n个组成单元 要不这n个组成单元同时成功 要不n个单元就同时失败 就是将n个组成单元放到一个事务中 2.mysql的事务 默认的 ...
- 外行人都能看懂的SpringCloud
一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...
- 用于解析通过JS的escape函数加密过的数据
function js_unescape($str) { $ret = ''; $len = strlen($str); for ($i = 0; $i < $len; $i++) { if ( ...
- hdu 1690 Bus System (有点恶心)
Problem Description Because of the huge population of China, public transportation is very important ...
- [APIO2007]风铃 --- 贪心
[APIO2007]风铃 题目描述 你准备给弟弟 Ike 买一件礼物,但是,Ike 挑选礼物的方式很特别:他只喜欢那些能被他排成有序形状的东西. 你准备给 Ike 买一个风铃.风铃是一种多层的装饰品, ...
- [ZROI 9.15模拟赛] Tutorial
Link: 传送门 可能要补一补之前的题了 题目名字天(Sky)的(De)炭(C)好评啊…… A: 从买/卖物品的配对来考虑: 可以发现如果当前物品为卖,肯定从之前选最小的(无论其为买/卖),因为贡献 ...