【淘寶ERP】DataGridView 表頭合併(RowSpan) 更新2020.01.07

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Drawing;
using System.Windows.Forms;


namespace ReportDataGridView
{


    public class DataGridViewHelper
    {
        public DataGridViewHelper(DataGridView gridview)
        {
            gridview.CellPainting += new DataGridViewCellPaintingEventHandler(gridview_CellPainting);
        }

        int top = 0;
        int left = 0;
        int height = 0;
        int width1 = 0;

        public void gridview_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            #region 重繪datagridview表頭
            DataGridView dgv = (DataGridView)(sender);
            if (e.RowIndex != -1) return;
            foreach (TopHeader item in Headers)
            {
                if (e.ColumnIndex >= item.Index && e.ColumnIndex < item.Index + item.Span)
                {
                    if (e.ColumnIndex == item.Index)
                    {
                        top = e.CellBounds.Top;
                        left = e.CellBounds.Left;
                        height = e.CellBounds.Height;
                    }
                    int width = 0;//總長度
                    for (int i = item.Index; i < item.Span + item.Index; i++)
                    {
                        width += dgv.Columns[i].Width;
                    }
                    Rectangle rect = new Rectangle(left, top, width, e.CellBounds.Height);
                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) //Cell背景顏色
                    {
                        //抹去原來的cell背景
                        e.Graphics.FillRectangle(backColorBrush, rect);
                    }
                    using (Pen gridLinePen = new Pen(dgv.GridColor)) //畫筆顏色
                    {
                        e.Graphics.DrawLine(gridLinePen, left, top, left + width, top);
                        e.Graphics.DrawLine(gridLinePen, left, top + height / 2, left + width, top + height / 2);
                        e.Graphics.DrawLine(gridLinePen, left, top + height - 1, left + width, top + height - 1); //自定義區域下部橫線
                        width1 = 0;
                        e.Graphics.DrawLine(gridLinePen, left - 1, top, left - 1, top + height);
                        for (int i = item.Index; i < item.Span + item.Index; i++)
                        {
                            if (i == 1 || i == 2)
                            {
                                width1 += dgv.Columns[i].Width - 1; //分隔區域首列
                            }
                            else
                            {
                                width1 += dgv.Columns[i].Width;
                            }
                            e.Graphics.DrawLine(gridLinePen, left + width1, top + height / 2, left + width1, top + height);
                        }
                        SizeF sf = e.Graphics.MeasureString(item.Text, e.CellStyle.Font);
                        float lstr = (width - sf.Width) / 2;
                        float rstr = (height / 2 - sf.Height) / 2;
                        //畫出文本框
                        if (item.Text != "")
                        {
                            e.Graphics.DrawString(item.Text, e.CellStyle.Font,
                                                        new SolidBrush(e.CellStyle.ForeColor),
                                                            left + lstr,
                                                            top + rstr,
                                                            StringFormat.GenericDefault);
                        }
                        width = 0;
                        width1 = 0;
                        for (int i = item.Index; i < item.Span + item.Index; i++)
                        {
                            string columnValue = dgv.Columns[i].HeaderText;
                            width1 = dgv.Columns[i].Width;
                            sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                            lstr = (width1 - sf.Width) / 2;
                            rstr = (height / 2 - sf.Height) / 2;
                            if (columnValue != "")
                            {
                                e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                            new SolidBrush(e.CellStyle.ForeColor),
                                                                left + width + lstr,
                                                                top + height / 2 + rstr,
                                                                StringFormat.GenericDefault);
                            }
                            width += dgv.Columns[i].Width;
                        }
                    }
                    e.Handled = true;
                }
            }
            #endregion
        }

        private List<TopHeader> _headers = new List<TopHeader>();
        public List<TopHeader> Headers
        {
            get { return _headers; }
        }

        public struct TopHeader
        {
            public TopHeader(int index, int span, string text)
            {
                this.Index = index;
                this.Span = span;
                this.Text = text;
            }
            public int Index;
            public int Span;
            public string Text;
        }
    }





}

 

 

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

using ReportDataGridView;

namespace AliWorkbenchProgram.View.TMReportFrm
{
    public partial class TestForm1 : Form
    {
        public TestForm1()
        {
            InitializeComponent();
        }

        private void TestForm1_Load(object sender, EventArgs e)
        {

            DataGridViewHelper helper = new DataGridViewHelper(this.dataGridView1);
            helper.Headers.Add(new DataGridViewHelper.TopHeader(2, 4, "總計"));

            this.dataGridView1.ColumnHeadersHeight = 50; //設置Dgv屬性ColumnHeadersHeightSizeMode纔會生效
            //ColumnHeadersHeightSizeMode設置爲"EnableResizing"

            DataTable dt = new DataTable();
            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");
            dt.Columns.Add("6");
            dt.Rows.Add("中國", "上海", "5000", "7000", "7000", "7000");
            dt.Rows.Add("中國", "北京", "3000", "5600", "7000", "7000");
            dt.Rows.Add("美國", "紐約", "6000", "8600", "7000", "7000");
            dt.Rows.Add("美國", "華勱頓", "8000", "9000", "7000", "7000");
            dt.Rows.Add("英國", "倫敦", "7000", "8800", "7000", "7000");

            this.dataGridView1.DataSource = dt;



        }
    }
}

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章