private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){// 对第1列相同单元格进行合并     if (e.ColumnIndex == 1 && e.RowIndex != -1 || e.ColumnIndex == 2 && e.RowIndex != -1){Brush datagridBrush = new SolidBrush(dataGridView1.GridColor);SolidBrush groupLineBrush = new SolidBrush(e.CellStyle.BackColor);using (Pen datagridLinePen = new Pen(datagridBrush)){// 清除单元格e.Graphics.FillRectangle(groupLineBrush, e.CellBounds);if (e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()){//绘制底边线e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);// 画右边线e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);}else{// 画右边线e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);}//对最后一条记录只画底边线if (e.RowIndex == dataGridView1.Rows.Count - 1){//绘制底边线e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);}// 填写单元格内容,相同的内容的单元格只填写第一个                        if (e.Value != null){if (e.RowIndex > 0 && dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString()){}else{//绘制单元格内容e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);}}e.Handled = true;}}}

1.清楚单元格样式。

2.重新绘制样式和字符串。

3.让列表显示为合并单元格的样式。

C#的DataGridView单元格合并相关推荐

  1. c# winform datagridview单元格合并

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

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

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

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

    #region <<单元格合并配置>>public struct MergeCellsParam{public int iStartCellColumn;public int ...

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

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

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

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

  6. cxgrid中纵横单元格合并_被合并单元格折磨疯的我,真后悔没早点知道这个Excel技巧!...

    在大家日常的工作中,经常会用到Excel合并单元格,然而合并单元格其实只是美化了表格,它会使我们后续的统计工作遇到很多麻烦,今天就给大家提供两个解决这个问题的思路. 01 合并单元格的基本操作方法 在 ...

  7. DataGridView 单元格自动填充

    在DataGridView单元格中,当输入指定字符时,自动完成填充. 通过 TextBox实现 AutoCompleteMode AutoCompleteMode.Suggest: AutoCompl ...

  8. DataGridView 单元格验证

    定义单元格验证 要求: 验证错误后焦点不离开. 实现: 单元格的验证可以使用dgv_details_CellValidating事件. 验证不通过时调用e.Cancel = true;终止事件链,单元 ...

  9. hssfworkbook 单元格合并后宽度不生效_Excel表格“假”合并,有多牛?

    在Excel表格中,你可以把多个单元格合并成一个单元格: 合并单元格只会保留左上角的单元格内容,为了证明这一点,你可以取消合并. 正是合并单元格这个只保留一个的特点,造成了表格后续的很多麻烦. 表格无 ...

最新文章

  1. 时间序列数据的预处理
  2. Python链接Mssql之Python库pymssql
  3. float浮动的学习
  4. SpringMVC4 返回Json数据
  5. ORACLE 11G RAC ASM磁盘全部丢失后的恢复
  6. j - 数据结构实验:哈希表_一看就懂的数据结构基础「哈希表」
  7. Scala调用Java静态成员及静态方法注意点——只能通过当前类名调用而不能借助子类调用父类静态成员/方法
  8. POJ 2240题(Floyd)
  9. 信号模型噪声服从零均值高斯分布_非高斯噪声下基于分数低阶循环谱的调制识别方法...
  10. linux python2.7 scipy_scipy.sparse.linalg.sp为Linux系统上的大型稀疏矩阵解决了令人惊讶的行为...
  11. poj1064 二分搜索 挑战程序设计竞赛
  12. linux日志删除1天前,Linux自动删除n天前日志
  13. 苹果确认部分iPad Air 3会出现永久性黑屏:可免费维修
  14. Java连接MySQL8.0以上版本数据库方式
  15. linux下如何挂载格式化内存卡,Linux下磁盘挂载格式化
  16. php 请求java_怎么php发送get请求给Java,然后返回想要的具体参数
  17. 紫光m300 路由器与磊科ni360 q360 nr235 ddwrt tomato等互刷折腾备忘
  18. python视频转图片
  19. android中的ems的真正含义
  20. 【IMP】IMP导入表的时候,如果表存在怎么办

热门文章

  1. springboot表单提交不支持put,delete
  2. ajax请求被SpringMVC拦截器拦截后如何进行页面跳转
  3. 锤子M1L安装google play记
  4. java在线考试管理系统整理
  5. java swing textfield_Java swing里怎么获得TextField手动输入的值?
  6. 基于CH375的智能数据采集卡设计分析
  7. Java编写胖老鼠的交易_HDOJ1009 肥鼠的交易 | 学步园
  8. mysql数据库中查看当前使用的数据库是哪个数据库?
  9. 自定义一个仿Spinner
  10. 金融壹账通发布上市后首份财报:营收23.28亿元,亏损16.61亿元