datagridview 头部单元格合并

本文参考自http://www.cnblogs.com/liuke209/archive/2006/07/29/463023.html,由于业务需要,只抽取了部分代码。

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

namespace OrderManager
{
    class DGVOper
    {
        #region 合并列时使用到的位置和大小属性
        int cTop = 0;//被合并表头区域的顶部坐标
        int cLeft = 0;//被合并表头区域的左边坐标
        /// <summary>
        /// 被合并表头区域的宽
        /// </summary>
        int cWidth = 0;
        int cHeight = 0;//。。。高
        #endregion      

        /// <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;
                if (e.ColumnIndex < 0)
                {
                    return;
                }
                string colName = dataGridView1.Columns[e.ColumnIndex].Name;
                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;                  

                        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(Color.White))// 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 + 10));
                        }
                    }        
                    #endregion
                    e.Handled = true;
                }
            }
        }
    }
}

方法调用:

        private void dgvOrder_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
                DGVOper exG = new DGVOper();

                //要合并的列名,注意是连续的
                List<string> colNameCollection = new List<string>();
                colNameCollection.Add("clmnPrintOrder");
                colNameCollection.Add("clmnConfirmOrder");

                exG.MergeHeader(sender, e, colNameCollection, "操作");
         }

转载于:https://www.cnblogs.com/stealyouheart/archive/2010/09/02/1816312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值