基本信息
源码名称:C#制作工资表等多种表格文档 Datagridview表头处理实例
源码大小:1.32M
文件格式:.zip
开发语言:C#
更新时间:2017-04-02
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

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

namespace TestDataGridView
{
    public partial class ExDGV : DataGridView
    {
        public ExDGV()
        {
            InitializeComponent();
        }

        #region 表头的属性构造
        private string[] _titleHeader=new string [0];
        public TopRow topRow;
        [Description("添加合并列表头,格式:合并列起始位置从1开始|列名称|占列个数")]
        public string[] TitleHeader
        {
            get { return this._titleHeader; }
            set
            { 
                this._titleHeader=value;
                this.SetHeader();
            }
        }
        #endregion

        #region TCell 单元格属性
        public class TCell
        {
            private int _ColSpan;
            private bool _IsColSpan;
            private bool _isMiddle;
            private int _RelateIndex;
            private int _SpanRowWith;
            private string _Text;
            public TCell()
            { }
            public int ColSpan
            {//跨度(合并格数)
                get { return this._ColSpan; }
                set { this._ColSpan = value; }
            }
            public bool IsColSpan
            {//是否要合并
                get { return this._IsColSpan; }
                set { this._IsColSpan = value; }
            }
            public int RelateIndex
            {
                get { return this._RelateIndex; }
                set { this._RelateIndex = value; }
            }
            public bool IsMiddle
            {
                get { return this._isMiddle; }
                set { this._isMiddle = value; }
            }
            public int SpanRowWith
            {
                get { return this._SpanRowWith; }
                set { this._SpanRowWith = value; }
            }
            public string Text
            {
                get { return this._Text; }
                set { this._Text = value; }
            }
        }
        #endregion

        #region TopRow
        public class TopRow
        {
            public ExDGV.TCell[] Cells;
            public TopRow(int colCount)
            {
                this.Cells = new TCell[colCount];
                for (int i = 0; i < colCount; i  )
                {
                    this.Cells[i] = new TCell();
                }
            }
        }
        #endregion

        #region setHeader
        /// <summary>
        /// 设置表头属性
        /// </summary>
        private void SetHeader()
        {
            if (this.TitleHeader != null &&TitleHeader.Length>0)
            {
                for (int i = 0; i < this.TitleHeader.GetLength(0); i  )
                {
                    string[] strArray = this.TitleHeader[i].Split(new char[] { '|' });
                    int index = Convert.ToInt32(strArray[0]) - 1;
                    this.topRow.Cells[index].Text = strArray[1];
                    this.topRow.Cells[index].ColSpan = Convert.ToInt32(strArray[2]);
                    this.topRow.Cells[index].IsColSpan = true;
                    for (int j = 1; j < Convert.ToInt32(strArray[2]); j  )
                    {
                        this.topRow.Cells[index   j].Text = strArray[1];
                        this.topRow.Cells[index   j].IsColSpan = true;
                        if (j != (Convert.ToInt32(strArray[2]) - 1))
                        {
                            this.topRow.Cells[index   j].IsMiddle = true;
                        }
                    }
                    this.topRow.Cells[(index   Convert.ToInt32(strArray[2])) - 1].RelateIndex = index;
                }
            }
            base.Invalidate();
        }
        #endregion

        /// <summary>
        /// 得到合并后列表头的宽度
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="self"></param>
        /// <returns></returns>
        private int GetSpanWidth(int idx,int self)
        {
            int num = 0;
            for (int i = idx   self; i < (idx   this.topRow.Cells[idx].ColSpan); i  )
            {
                num  = base.Columns[i].Width;
            }
            return num;
        }

        #region 重载列数增加事件
        protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
        {
            this.topRow = new TopRow(base.Columns.Count);
            base.OnColumnAdded(e);
        }
        #endregion

        #region 单元格重绘事件
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (((this.TitleHeader!=null) && (e.RowIndex==-1)) && (this.TitleHeader.GetLength(0)>0))
            {
                new Rectangle(e.CellBounds.X 1, e.CellBounds.Y   1, e.CellBounds.Width - 4, e.CellBounds.Height - 4);
                using (Brush brush=new SolidBrush(base.GridColor))
                {
                    using (Brush brush2=new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen pen = new Pen(brush))
                        {
                            e.Graphics.FillRectangle(brush2, e.CellBounds);
                            e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            if (((e.ColumnIndex > -1) && (this.topRow != null)) && ((this.topRow.Cells[e.ColumnIndex].ColSpan > 1) || this.topRow.Cells[e.ColumnIndex].IsMiddle))
                            {
                                e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top   e.ClipBounds.Height / 2, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                            }
                            else
                            {//画整条竖线
                                e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                            }
                            int num1 = e.CellBounds.Height / 3;
                            if ((e.ColumnIndex > -1) && this.topRow.Cells[e.ColumnIndex].IsColSpan)
                            {//画中线
                                int num2 = e.CellBounds.Height / 2;
                                e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - (e.CellBounds.Height / 2), e.CellBounds.Right, e.CellBounds.Bottom - (e.CellBounds.Height / 2));
                            }
                            if (((e.ColumnIndex > -1) && (this.topRow.Cells[e.ColumnIndex].RelateIndex > 0)) && (this.topRow.Cells[e.ColumnIndex].Text != null))
                            {//写合并列表头的名称
                                Rectangle layoutRectangle = new Rectangle((e.CellBounds.X - 1) - this.GetSpanWidth(this.topRow.Cells[e.ColumnIndex].RelateIndex, 1), e.CellBounds.Y   5, this.GetSpanWidth(this.topRow.Cells[e.ColumnIndex].RelateIndex, 0), e.CellBounds.Height / 2);
                                StringFormat format = new StringFormat();
                                format.Alignment = StringAlignment.Center;
                                e.Graphics.DrawString(this.topRow.Cells[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Crimson, layoutRectangle, format);
                            }
                            float y = 0f;
                            if (e.ColumnIndex >= 0)
                            {
                                if (this.topRow.Cells[e.ColumnIndex].IsColSpan)
                                {
                                    y = (e.CellBounds.Y   (e.CellBounds.Height / 2))   (((e.CellBounds.Height / 2) - e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height) / 2f);
                                }
                                else
                                {
                                    y = e.CellBounds.Y   ((e.CellBounds.Height - e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height) / 2f);
                                }
                                if (e.Value != null)
                                {
                                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X   ((e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width) / 2f), y, StringFormat.GenericDefault);
                                }
                            }
                            e.Paint(e.ClipBounds, DataGridViewPaintParts.SelectionBackground | DataGridViewPaintParts.Focus | DataGridViewPaintParts.ErrorIcon | DataGridViewPaintParts.ContentBackground);
                            e.Handled = true;
                        }
                    }
                }
            }
            base.OnCellPainting(e);
        }
        #endregion

        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: 在此处添加自定义绘制代码
            //pe.Graphics.TranslateTransform(, this.AutoScrollPosition.Y);//确保滚动时的相对坐标正确
            // 调用基类 OnPaint
            base.OnPaint(pe);
        }
        
    }
}