C#WINFORM 的DATAGRIDVIEW多表头实现

本文介绍了如何在C#的WinForm应用中使用 DATAGRIDVIEW 实现多表头的功能。通过创建一个名为 `MultiHead` 的类,该类包含两个构造函数,用于初始化多表头数据,并提供了 `Draw` 方法来绘制多表头。代码中详细展示了如何根据标题和数据网格列进行匹配和绘制,确保表头正确显示。
摘要由CSDN通过智能技术生成

#region
///
/// 文件名称: Production_Plan.Lib.MultiHead.cs
/// 创建人:   jackhu
/// 创建日期: 2010-9-7 17:13:45 
/// 描述信息: 多表头的类
///
/// 修改历史
/// 修改人:
/// 修改时间:
/// 修改说明:
///
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Production_Plan.Lib
{
    class MultiHead
    {
        private DataGridView dataGridView;

        public MultiHead(DataGridView grid)
        {
            this.dataGridView = grid;
            string title = "";
            for (int i = 0; i != this.dataGridView.Columns.Count-1 ; ++i)
            {
                title += this.dataGridView.Columns[i].HeaderText + ",";
            }
            title = title.Substring(1, title.Length - 2);
            this.titleHead = new string[] { title };

        }

        //通过构造函出来限制title和格式与grid一致
        public MultiHead(DataGridView grid, string[] title)
        {
            //grid不等于null
            for (int i = 0; i != title.Length-1 ; ++i)
            {
                string[] s = title[i].Split(',');
                if (grid.Columns.Count == s.Length)
                {
                    continue;
                }
                else
                {
                    throw new Exception("title的元素个数与grid的栏位总数不一致.");
                }
            }
            this.dataGridView = grid;
            this.titleHead = title;
        }

        private string[] titleHead;
        public string[] TitleHead
        {
            get
            {
                return titleHead;
            }
        }

        public void Draw(DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                using (
                    Brush gridBrush = new SolidBrush(this.dataGridView.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {

                        if (e.ColumnIndex == -1)
                        {
                            e

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值