datagridview合并表头

1.功能说明:

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

2.不足之处:

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

 

3.使用参考.

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

  1. Utility.exGridView.isEnLarged = false;

datagridviewcellpaiting事件中写如下代码

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

4.效果截图

 

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

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. namespace Utility
  7. {
  8.     public class exGridView
  9.     {
  10.         #region 合并列时使用到的位置和大小属性
  11.         int cTop = 0;//被合并表头区域的顶部坐标
  12.         int cLeft = 0;//被合并表头区域的左边坐标
  13.         /// <summary>
  14.         /// 被合并表头区域的宽
  15.         /// </summary>
  16.         int cWidth = 0;
  17.         int cHeight = 0;//。。。高
  18.         #endregion 
  19.         /// <summary>
  20.         /// 判断是否已经将datagridview的表头变高了,只增高一次。
  21.         /// </summary>
  22.         public static bool isEnLarged = false;
  23.         /// <summary>
  24.         /// 合并表头,用在dataGridView的CellPainting事件中。
  25.         /// </summary>
  26.         /// <param name="sender">需要重绘的dataGridview</param>
  27.         /// <param name="e">CellPainting中的参数</param>
  28.         ///<param name="colName">列的集合(列必须是连续的,第一列放在最前面)</param>
  29.         /// <param name="headerText">列合并后显示的文本</param>
  30.         public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e,List<string> colNameCollection,string headerText)
  31.         {
  32.             if (e.RowIndex == -1)
  33.             {
  34.                 DataGridView dataGridView1=sender as DataGridView;
  35.                 if (e.ColumnIndex <0)
  36.                {
  37.                     return;
  38.                }
  39.                 string colName = dataGridView1.Columns[e.ColumnIndex].Name;
  40.                 if (!isEnLarged)
  41.                 {
  42.                     //0.扩展表头高度为当前的2倍
  43.                     dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  44.                     dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2;
  45.                     isEnLarged = true;
  46.                 }
  47.                 if (colNameCollection.Contains(colName))
  48.                 {
  49.                     #region 重绘列头
  50.                     //1.计算colLen个列的区域
  51.                     if (colNameCollection.IndexOf(colName)==0)
  52.                     {
  53.                         cTop = e.CellBounds.Top;
  54.                         cLeft = e.CellBounds.Left;                        
  55.                         cWidth = e.CellBounds.Width;
  56.                         cHeight = e.CellBounds.Height/2;
  57.                         foreach(string colNameItem in colNameCollection)
  58.                         {
  59.                             if (colNameItem.Equals(colName))
  60.                             {//除去自己一个,加了之后colLen-1个列的宽
  61.                                 continue;
  62.                             } 
  63.                             cWidth += dataGridView1.Columns[colNameItem].Width;                           
  64.                         }                       
  65.                     }
  66.                     Rectangle cArea = new Rectangle(cLeft, cTop, cWidth, cHeight);
  67.                     //2.把区域设置为背景色,没有列的分线及任何文字。
  68.                     using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
  69.                     {
  70.                         e.Graphics.FillRectangle(backColorBrush, cArea);
  71.                     }
  72.                     //3.绘制新列头的边框
  73.                     using (Pen gridPen = new Pen(dataGridView1.GridColor))
  74.                     {
  75.                         //3.1 上部边框
  76.                         e.Graphics.DrawLine(gridPen, cLeft, cTop, cLeft + cWidth, cTop);
  77.                         using (Pen hilightPen = new Pen(Color.WhiteSmoke))
  78.                         {
  79.                             //3.2 顶部高光
  80.                             e.Graphics.DrawLine(hilightPen, cLeft, cTop + 1, cLeft + cWidth, cTop + 1);
  81.                             //3.3 左部反光线
  82.                             e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3, cLeft, cTop + cHeight - 2);
  83.                         }
  84.                         //3.4 下部边框
  85.                         e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1, cLeft + cWidth, cTop + cHeight - 1);
  86.                         //3.5 右部边框                        
  87.                         e.Graphics.DrawLine(gridPen, cLeft + cWidth - 1, cTop, cLeft + cWidth - 1, cTop + cHeight);//(cTop+cHeight)/2);
  88.                     }
  89.                     //4.写文本
  90.                     if (colNameCollection.IndexOf(colName) == 0)
  91.                     {//不是第一列则不写文字。
  92.                         int wHeadStr = (int)(headerText.Length * e.CellStyle.Font.SizeInPoints);
  93.                         int wHeadCell = cWidth;
  94.                         int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;
  95.                         using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
  96.                         {
  97.                             e.Graphics.DrawString(headerText, e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));
  98.                         }
  99.                     }
  100.                    
  101.                     //5 绘制子列背景
  102.                     int FatherColHeight = e.CellBounds.Height / 2;//上面一行的高度 
  103.                     using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
  104.                     {
  105.                         e.Graphics.FillRectangle(backColorBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + FatherColHeight, e.CellBounds.Width - 1, e.CellBounds.Height / 2 - 1));
  106.                     }
  107.                     //5.1绘制子列的边框                   
  108.                     using (Pen gridPen = new Pen(dataGridView1.GridColor))
  109.                     {
  110.                         using (Pen hilightPen = new Pen(Color.WhiteSmoke))
  111.                         {
  112.                             //5.2 左部反光线
  113.                             e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3 + FatherColHeight, cLeft, cTop + cHeight - 2 + FatherColHeight);
  114.                         }
  115.                         //5.3 下部边框
  116.                         e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1 + FatherColHeight, cLeft + cWidth, cTop + cHeight - 1 + FatherColHeight);
  117.                         //5.4 右部边框 
  118.                         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);                    
  119.                     }
  120.                     //5.5 写子列的文本
  121.                     int wStr = (int)(dataGridView1.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints);
  122.                     int wCell = e.CellBounds.Width;
  123.                     int pLeft = (wCell - wStr) / 2;//相对CELL左边框的左坐标
  124.                     using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
  125.                     {
  126.                         e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, foreBrush, new PointF(e.CellBounds.X + pLeft, cTop + 3 + FatherColHeight));
  127.                     }                    
  128.                     #endregion
  129.                     e.Handled = true;
  130.                 }
  131.             }
  132.         }       
  133.     }
  134. }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值