#region <<单元格合并配置>>public struct MergeCellsParam{public int iStartCellColumn;public int iStartCellRow;public int iEndCellColumn;public int iEndCellRow;public string strText;public MergeCellsParam(int _iStartColumn, int _iStartRow, int _iEndColumn, int _iEndRow){iStartCellColumn = _iStartColumn;iStartCellRow = _iStartRow;iEndCellColumn = _iEndColumn;iEndCellRow = _iEndRow;strText = null;}}public List<MergeCellsParam> mMergeCells = new List<MergeCellsParam>();
#endregion
 #region 获取单元格合并所需的行列public void UpdateMergeCells(){mMergeCells.Clear();int Sum = 0;for (int i = 0; i < sql.Upname.Count; i++){if (sql.RootCount[i] > 1){mMergeCells.Add(new MergeCellsParam(0, Sum, 0, Sum + sql.RootCount[i] - 1));mMergeCells.Add(new MergeCellsParam(1, Sum, 1, Sum + sql.RootCount[i] - 1));Sum += sql.RootCount[i];}elseSum++;}}
#endregion
#region <<单元格合并>>private void GridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){for (int i = 0; i < mMergeCells.Count; i++){if (mMergeCells[i].iStartCellRow == mMergeCells[i].iEndCellRow&& mMergeCells[i].iStartCellColumn == mMergeCells[i].iEndCellColumn){return;}var _mergeParam = mMergeCells[i];if (e.ColumnIndex >= _mergeParam.iStartCellColumn && e.ColumnIndex <= _mergeParam.iEndCellColumn&& e.RowIndex >= _mergeParam.iStartCellRow && e.RowIndex <= _mergeParam.iEndCellRow){FindRange(e, this.GridView);}}}
#endregion
     #region 找到上下行内容相同的行private void FindRange(DataGridViewCellPaintingEventArgs e, DataGridView myGrid){if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet){e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;}Brush gridBrush = new SolidBrush(GridView.GridColor);SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);int UpRows = 0;int DownRows = 0;int count = 0;int cellwidth = e.CellBounds.Width;Pen gridLinePen = new Pen(gridBrush);if (e.Value != null){string curValue = e.Value == null ? "" : e.Value.ToString().Trim();//MessageBox.Show("curValue:" + curValue);#region 获取下面的行数for (int i = e.RowIndex; i < myGrid.Rows.Count; i++){if (myGrid.Rows[i].Cells[e.ColumnIndex].Value != null){if (myGrid.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue)){//this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;DownRows++;if (e.RowIndex != i){cellwidth = cellwidth < myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width;}}else{break;}}}#endregion#region 获取上面的行数for (int i = e.RowIndex; i > 0; i--){if (myGrid.Rows[i].Cells[e.ColumnIndex].Value != null){if (myGrid.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue)){//this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;UpRows++;if (e.RowIndex != i){cellwidth = cellwidth < myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : myGrid.Rows[i].Cells[e.ColumnIndex].Size.Width;}}else{break;}}}count = DownRows + UpRows - 1;if (count < 1){return;}}//if (GridView.Rows[e.RowIndex].Selected)// {//backBrush.Color = e.CellStyle.SelectionBackColor;backBrush.Color = Color.White;fontBrush.Color = e.CellStyle.SelectionForeColor;// }//以背景色填充e.Graphics.FillRectangle(backBrush, e.CellBounds);//画字符串if(e.RowIndex < sql.RootCount[0]){PaintingFont(e, cellwidth, UpRows, DownRows, count-1);}elsePaintingFont(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;#endregion}
#endregion
#region 画字符串// cellwidth = e.CellBounds.Width;// UpRows上面相同的行数// DownRows下面相同的行数// count 总行数private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count){SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);int fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;int fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;int cellheight = e.CellBounds.Height;if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter){e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);}}
#endregion

C# DataGridView单元格合并与文字居中相关推荐

  1. POI-HSSFWorkbook合并单元格边框及文字居中问题

    POI-HSSFWorkbook合并单元格边框及文字居中问题 首先创建一个需要合并单元格的样式 //合并的单元格样式 HSSFCellStyle boderStyle = wb.createCellS ...

  2. c# winform datagridview单元格合并

    效果 /// <summary>     /// DataGridView 单元格合并信息     /// </summary>     public struct DataG ...

  3. C# DataGridView单元格合并居中

    利用CellPainting事件 方法说明:先将原始表格的单元格框线清除,再利用事件对需要显示的框线进行重新绘制,达到单元格合并的效果. int index = 0; // 用于得到合并后文字应该显示 ...

  4. datagridview单元格合并居中_系统地学习Excel第17课,设置单元格格式

    上一篇:系统地学习Excel第16课,使用Excel的「替换」功能 本篇内容结构如下: 本章的知识体系 Excel工作表的整体外观由各个单元格的样式构成,单元格的样式外观在Excel的可选设置中主要包 ...

  5. DataGridView为指定的区域合并单元格并设置文字居中

    DataGridView合并单元格 传参结构体 配置结构体成员参数 单元格合并 找到上下行内容相同的行 写入字符串 实验 效果 传参结构体 #region <<单元格合并配置>> ...

  6. C#实例:datagridview单元格合并

    这是替C#微信交流群群友做的一个小实例,目的就是在datagridview选择对应行以后,点击button后获取对应行的ip,并执行相应的操作,其实我觉得这样的话button没必要非放置到datagr ...

  7. C#的DataGridView单元格合并

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){// 对第1列相 ...

  8. 2.单元格的跨列居中和自动换行

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 单元格的跨列居中和自动换行 前言 一.跨列居中 二.自动换行 前言 制作表格时,为了不影响后续的数据处理与分析,可以使用跨列居中功能代 ...

  9. Python 技术篇 - 操作excel实现单元格合并并居中实例演示,用openpyxl库为指定区域的单元格设置对齐样式和字体样式方法

    Openpyxl 设置 excel 单元格合并.对齐样式和字体 第一章:openpyxl 操作 excel ① 设置单元格合并 ② 设置单元格居中对齐样式 ③ 设置单元格字体样式 第一章:openpy ...

最新文章

  1. 水磨石地面分隔条设置示意图_水磨石抛光过程中什么时候用百洁垫?什么时候用百亮钢丝棉?...
  2. Linux命令备忘录: jobs 显示Linux中的任务列表及任务状态命令
  3. gm怎么刷东西 rust_刷了这种黑板漆再也不用担心吃粉笔灰了
  4. Linux 安装 Elasticsearch-rtf
  5. zabbix 2.2 监控mysql_Zabbix-2.2.2监控MySQL的复制-阿里云开发者社区
  6. spring14-----AOP之通知参数
  7. 【华为云技术分享】云小课 | OBS提供多方面数据安全保障,让存储放心、贴心、省心
  8. 浅析.NET中的引用类型和值类型(上)
  9. OpenGL基础49:高度贴图(下)
  10. PHP下SESSION无法跨页传递的解决
  11. 【深度学习论文】:U-Net
  12. 老人为啥怒砸30辆车
  13. C# Winform 开发框架
  14. face_alignment库获取人脸图片landmark示例
  15. 网上评卷系统连接不到服务器,在网上阅卷系统过程中可能存在的问题及解决方法...
  16. Typora 官网下载
  17. 中国集成灶10大品牌排行榜揭晓,公认的集成灶10大品牌是哪几个?
  18. mac下Xshell和Xftp | Royal TSX
  19. Springboot集成海康SDK(以海康USB_SDK为例)
  20. 物品怎么抠图?手把手教你快速抠图

热门文章

  1. 【JVM】JVM监控
  2. python 安装matplotlib error:could not find a version that satisfies the requirement xxx
  3. “十一”期间支付系统也“休假”
  4. 计算机数据库基础知识笔记,2012年计算机二级Access数据库基础知识笔记(8)
  5. 南科大计算机专业博士授予权,计算机科学与技术、数学2个一级学科博士授予权正式获批...
  6. python爬取知乎回答并进行舆情分析:舆情分析部分
  7. Excel中四舍六入五成双的最佳函数算法
  8. Excel中的四舍六入五成双
  9. 微信小程序滑动导航栏
  10. HANA Excel 连接HANA DB