datagridview合并表头

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: datagridview表头是用来显示列名的,默认情况下是可以看到的。如果你需要禁止表头的显示,可以通过以下步骤实现: 1. 打开窗体的设计视图,在DataGridView控件的属性窗口中找到Columns属性。 2. 单击Columns属性旁边的“…”按钮,打开列编辑器。 3. 在编辑器中,为每一列设置HeaderText属性为相应的列名,这样在表头处就会显示列名。 4. 在DataGridView的属性窗口中,找到ColumnHeadersVisible属性,将其值设置为False,这样表头就被隐藏起来了。 这样做之后,你将看到在运行时表头不再显示,只有数据行可见。请注意,这只是隐藏了表头的显示,并不会删除或者改变表头的结构,因此你仍然可以通过代码或者其他方式来操作和访问表头。 ### 回答2: 要禁止 DataGridView表头,可以使用以下方法: 1. 设置 DataGridView 的 ColumnHeadersVisible 属性为 False。这将隐藏整个表头,并且禁用了对表头的排序、拖动列等操作。代码示例: DataGridView1.ColumnHeadersVisible = False 2. 如果你只想禁止用户拖动列而保留表头的显示和其他操作,可以使用 ColumnHeadersHeightSizeMode 属性来禁用拖动功能。代码示例: DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing 3. 如果你希望保留表头的显示,但要禁用所有的表头操作,可以使用 HeaderCell 属性来禁用鼠标事件并移除掉所有的 ContextMenuStrip。代码示例: For Each col As DataGridViewColumn In DataGridView1.Columns col.HeaderCell = New DataGridViewColumnHeaderCell() With { .Value = col.HeaderText, .ToolTipText = col.HeaderText, .ContextMenuStrip = Nothing } Next 无论使用哪种方法,都可以有效地禁止 DataGridView表头操作。根据具体的需求,选择适合的方法即可。 ### 回答3: DataGridView是.NET Framework中的一个控件,用于展示和编辑数据。如果要禁用DataGridView表头,可以使用以下方法: 1. 设置DataGridView的ColumnHeadersVisible属性为False。这将隐藏掉整个表头部分,包括列名和排序按钮。 2. 使用样式设置隐藏表头。首先,可以通过设置DataGridView的EnableHeadersVisualStyles属性为False,禁用默认的表头样式。然后,可以通过修改DataGridView的DefaultCellStyle属性来隐藏表头的外观,比如将表头的字体颜色设置为与背景颜色相同。 3. 使用尺寸调整行隐藏表头。在DataGridView的RowHeadersVisible属性为False的情况下,可以通过将表头行的高度设置为0,来隐藏掉表头。这样表头行将不可见,但是单元格内容仍然可以正常显示和编辑。 总结起来,禁止DataGridView表头可以通过隐藏整个表头、修改样式或调整行高度等方式来实现。具体选择哪种方式取决于实际需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值