C#WINFORM 的DATAGRIDVIEW多表头实现

#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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值