1.功能说明:

将连续的多个列合并成一个新列。

2.不足之处:

不能合并多层。比如下图这样的功能是没有的。

3.使用参考.

在form的构造函数里写下如下代码

Utility.exGridView.isEnLarged = false;
在datagridview的cellpaiting事件中写如下代码

Utility.exGridView exG = new Utility.exGridView();
List colNameCollection=new List();
for (int i = 0; i < 10; i++)
{
//"colDraw"+i.ToString()是columnName的属性值
colNameCollection.Add("colDraw" + i.ToString());
}
exG.MergeHeader(sender, e, colNameCollection, "0-9中奖号码分布图");
4.效果截图

5.源文件(没找到添加附件的地方,就贴出代码了)

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Utility
{
    public class exGridView
    {
        #region 合并列时使用到的位置和大小属性
        int cTop = 0;//被合并表头区域的顶部坐标
        int cLeft = 0;//被合并表头区域的左边坐标
        /// <summary>
        /// 被合并表头区域的宽
        /// </summary>
        int cWidth = 0;
        int cHeight = 0;//。。。高
        #endregion 
        /// <summary>
        /// 判断是否已经将datagridview的表头变高了,只增高一次。
        /// </summary>
        public static bool isEnLarged = false;

/// <summary>
        /// 合并表头,用在dataGridView的CellPainting事件中。
        /// </summary>
        /// <param name="sender">需要重绘的dataGridview</param>
        /// <param name="e">CellPainting中的参数</param>
        ///<param name="colName">列的集合(列必须是连续的,第一列放在最前面)</param>
        /// <param name="headerText">列合并后显示的文本</param>
        public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e,List<string> colNameCollection,string headerText)
        {
            if (e.RowIndex == -1)
            {
                DataGridView dataGridView1=sender as DataGridView;
                string colName = dataGridView1.Columns[e.ColumnIndex].Name;
                if (!isEnLarged)
                {
                    //0.扩展表头高度为当前的2倍
                    dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                    dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2;
                    isEnLarged = true;
                }
                if (colNameCollection.Contains(colName))
                {
                    #region 重绘列头
                    //1.计算colLen个列的区域
                    if (colNameCollection.IndexOf(colName)==0)
                    {
                        cTop = e.CellBounds.Top;
                        cLeft = e.CellBounds.Left;

cWidth = e.CellBounds.Width;
                        cHeight = e.CellBounds.Height/2;

foreach(string colNameItem in colNameCollection)
                        {
                            if (colNameItem.Equals(colName))
                            {//除去自己一个,加了之后colLen-1个列的宽
                                continue;
                            } 
                            cWidth += dataGridView1.Columns[colNameItem].Width;                           
                        }                       
                    }

Rectangle cArea = new Rectangle(cLeft, cTop, cWidth, cHeight);
                    //2.把区域设置为背景色,没有列的分线及任何文字。
                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        e.Graphics.FillRectangle(backColorBrush, cArea);

}
                    //3.绘制新列头的边框
                    using (Pen gridPen = new Pen(dataGridView1.GridColor))
                    {
                        //3.1 上部边框
                        e.Graphics.DrawLine(gridPen, cLeft, cTop, cLeft + cWidth, cTop);
                        using (Pen hilightPen = new Pen(Color.WhiteSmoke))
                        {
                            //3.2 顶部高光
                            e.Graphics.DrawLine(hilightPen, cLeft, cTop + 1, cLeft + cWidth, cTop + 1);
                            //3.3 左部反光线
                            e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3, cLeft, cTop + cHeight - 2);
                        }
                        //3.4 下部边框
                        e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1, cLeft + cWidth, cTop + cHeight - 1);
                        //3.5 右部边框                        
                        e.Graphics.DrawLine(gridPen, cLeft + cWidth - 1, cTop, cLeft + cWidth - 1, cTop + cHeight);//(cTop+cHeight)/2);
                    }
                    //4.写文本
                    if (colNameCollection.IndexOf(colName) == 0)
                    {//不是第一列则不写文字。
                        int wHeadStr = (int)(headerText.Length * e.CellStyle.Font.SizeInPoints);
                        int wHeadCell = cWidth;
                        int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;
                        using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
                        {
                            e.Graphics.DrawString(headerText, e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));
                        }
                    }

//5 绘制子列背景
                    int FatherColHeight = e.CellBounds.Height / 2;//上面一行的高度 
                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        e.Graphics.FillRectangle(backColorBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + FatherColHeight, e.CellBounds.Width - 1, e.CellBounds.Height / 2 - 1));
                    }
                    //5.1绘制子列的边框                   
                    using (Pen gridPen = new Pen(dataGridView1.GridColor))
                    {
                        using (Pen hilightPen = new Pen(Color.WhiteSmoke))
                        {
                            //5.2 左部反光线
                            e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3 + FatherColHeight, cLeft, cTop + cHeight - 2 + FatherColHeight);
                        }
                        //5.3 下部边框
                        e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1 + FatherColHeight, cLeft + cWidth, cTop + cHeight - 1 + FatherColHeight);

//5.4 右部边框 
                        e.Graphics.DrawLine(gridPen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + FatherColHeight, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + e.CellBounds.Height + FatherColHeight);//(cTop+cHeight)/2);

}
                    //5.5 写子列的文本
                    int wStr = (int)(dataGridView1.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints);
                    int wCell = e.CellBounds.Width;
                    int pLeft = (wCell - wStr) / 2;//相对CELL左边框的左坐标

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
                    {
                        e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, foreBrush, new PointF(e.CellBounds.X + pLeft, cTop + 3 + FatherColHeight));
                    }                    
                    #endregion
                    e.Handled = true;
                }
            }
        }       
    }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/debug1984/archive/2008/11/08/3253858.aspx

转载于:https://www.cnblogs.com/fx_guo/archive/2010/06/07/1935615.html

datagridview合并表头相关推荐

  1. DataGridView合并表头实现 、二维表头的实现

    ASP.net下實現合并單元格相對簡單,很大程度上是因為基於HTML:Winform下的DataGridView實現比較繁瑣,目前看似只有重繪這條路:如果嫌累可以使用Developer Express ...

  2. winform 中 给DataGridView的表头添加CheckBox

    在C/S架构中,给DataGridView的表头添加CheckBox控件: 添加类: /// <summary> /// 给DataGridView添加全选 /// </summar ...

  3. WPF Datagrid合并表头的思路

    在使用datagrid的时候,有很多情况下,都需要合并表头,多行表头之类的操作.这就需要我们自定义列了. 本文给出一个思路,可以实现此需要,只是本人对这个研究不很明白,只是只是实现,仅此而已. 下面是 ...

  4. element的table组件,表头合并(合并表头单元格)

    在el-table-column里面写两个el-table-column,然后用header-cell-style将子表头隐藏 <el-table:data="tableData&qu ...

  5. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)

    1.不用多说了,先上图片 2.功能特性 1.表头的背景色,标题的虚线边框 2.表头的个别单元格样式 3. 表头合并行功能 4. 复杂的动态表头功能 5. 表体渲染下标从第n行开始 6. 表体第一行,第 ...

  6. POI导出支持合并表头及单元格锁定的Excel

    1.开发前提 目前项目上有这样的需求,支持Excel导出后再线下调至后导入,并且有关键字及其他部分字段不支持修改,所以需要写保护,按正常的理解来讲锁定特定的列即可,但是需求中需要在Excel中添加行数 ...

  7. el-table合并表头handerMethod

    项目中使用element的table表格,总是需要一些自定义的要求,虽然element已经提供了很多方法,但是如何使用还是需要自己探索 先上效果截图 这里主要使用到:header-cell-style ...

  8. jqGrid合并表头

    刚入职新公司,负责的第一个项目就是前后端不分离的后台管理项目;其中使用的表格框架就是jqGrid,特此记录;(前后端不分离调试起来是真的抓狂!!) 不浪费各位时间,先上效果图: 这是我在本地方便调试运 ...

  9. EasyExcel导出excel合并表头和数据

    maven依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</ ...

最新文章

  1. IDEA中maven项目导jar包太慢
  2. 画出18*18的棋盘以及用不同颜色绘制出同心圆(python实现)
  3. 为什么越来越多的程序员开始学机器学习的原因
  4. 卸掉包袱,诺基亚将走得更远
  5. Spring MVC+MyBatis中Spring没有接管Mybatis的事务
  6. Html图片懒加载动画,带加载进度的Web图片懒加载组件Lazyload
  7. python3种基本数字类型_Python3基本数据类型
  8. 想成功创业,你首先要有这样的思维
  9. linux文件系统ext2\ext3\ext4\xfs详解
  10. MSN退休,寻找微软如何融合MSN的答案:体验skype6国际版MSN功能。
  11. 通过qq进行ip定位
  12. OFD文件预览—前后端两种实现方式
  13. 腾讯微博qq说说备份导出工具_10年过去了,腾讯微博终于被判了死刑。。。
  14. 京东程序员压力太大在网页植入骂人代码?网友:。。。
  15. UE5 Oculus Quest2 使用跨平台交互语音聊天Cross-Platform Voice Chat Pro制作语音聊天功能 1
  16. linux 挂载fat32格式u盘
  17. php做excel导入word,php如何将excel表格插入到word文档
  18. np.cosh没有分配_JavaScript中带有示例的Math.cosh()方法
  19. vue跳转链接(新页签)
  20. 【项目】磁盘文件管理工具

热门文章

  1. 51单片机sht30_基于51单片机和机智云的WIFI智能插座(2019版)
  2. Lc24两两交换链表
  3. different intergers
  4. Fisher算法+两类问题
  5. [leetcode]169. 多数元素
  6. upper_bound( )和lower_bound()的用法
  7. 纠错编码--海明码(动一发而牵全身)
  8. PBR理论基础2:光照、材质与微面元理论
  9. SPFA差分约束(bzoj 2330: [SCOI2011]糖果)
  10. [debug] RuntimeError: “nll_loss_forward_reduce_cuda_kernel_2d_index“ not implemented for ‘Int‘