怎么实现.NET下的DataGridView合并列表头

上周发了一篇《为了做低代码平台,这些年我们对.NET的DataGridView做的那些扩展》,展示了我们为了开发低代码所以对DataGridView控件做的一些扩展,然后有些朋友希望能够给出具体的实现,所以把相关代码抽取出来,先描述下怎么实现“合并列表头”的功能。

先上效果图往往是最好的:

效果图

 

实现其实很简单,主要做了两点:

1、扩展DataGridViewColumn;

由于要合并列表头,那么我们需要一些额外的扩展信息:合并后的HeaderText,如上图中的“本月业绩”和“上月业绩”、合并列数、以及单元格是否属于合并单元格,比如上图中的“订单数”这一列,那需要能够设置合并后的HeaderText,也需要设置合并列数。但是“订单金额”列,显然只需要知道自己属于某个合并列就行。

 扩展了一个DataGridViewExt列

2、扩展DataGridViewColumnHeaderCell;

扩展HeaderCell的目的是要在Paint中重新绘制HeaderCell,所以主要代码在override的Paint方法中。

 代码比较简单,为了各位能够拷贝就能使用,直接把代码贴上来,希望能够帮助到您:

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

namespace eHelper.UnitTest
{
    public partial class DataGridViewTestForm : Form
    {
        private DataGridViewColumnExt SalesName;
        private DataGridViewColumnExt MonthOrders;
        private DataGridViewColumnExt MonthAmt;
        private DataGridViewColumnExt PreMonthOrders;
        private DataGridViewColumnExt PreMonthAmt;

        public DataGridViewTestForm()
        {
            InitializeComponent();
            InitDataGridView();
        }

        private void InitDataGridView()
        {
            this.DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.DataGridView.ColumnHeadersHeight = 42;

            this.SalesName = new DataGridViewColumnExt();
            this.MonthOrders = new DataGridViewColumnExt();
            this.MonthAmt = new DataGridViewColumnExt();
            this.PreMonthOrders = new DataGridViewColumnExt();
            this.PreMonthAmt = new DataGridViewColumnExt();

            this.SalesName.FillWeight = 200F;
            this.SalesName.HeaderText = "业务员姓名";
            this.SalesName.Name = "SalesName";
            this.SalesName.Width = 200;

            this.MonthOrders.FillWeight = 100F;
            this.MonthOrders.HeaderText = "订单数";
            this.MonthOrders.Name = "MonthOrders";
            this.MonthOrders.Width = 100;
            this.MonthOrders.HeaderExpandedColumns = 2;
            this.MonthOrders.HeaderExpandedColumnsText = "本月业绩";

            this.MonthAmt.FillWeight = 100F;
            this.MonthAmt.HeaderText = "订单金额";
            this.MonthAmt.Name = "MonthAmt";
            this.MonthAmt.Width = 100;

            this.PreMonthOrders.FillWeight = 100F;
            this.PreMonthOrders.HeaderText = "订单数";
            this.PreMonthOrders.Name = "PreMonthOrders";
            this.PreMonthOrders.Width = 100;
            this.PreMonthOrders.HeaderExpandedColumns = 2;
            this.PreMonthOrders.HeaderExpandedColumnsText = "上月业绩";

            this.PreMonthAmt.FillWeight = 100F;
            this.PreMonthAmt.HeaderText = "订单金额";
            this.PreMonthAmt.Name = "PreMonthAmt";
            this.PreMonthAmt.Width = 100;

            this.DataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.SalesName,
            this.MonthOrders,
            this.MonthAmt,
            this.PreMonthOrders,
            this.PreMonthAmt});
        }

        public class DataGridViewColumnExt : DataGridViewTextBoxColumn
        {
            public DataGridViewColumnExt()
            {
                this.HeaderCell = new DataGridViewColumnHeaderCellExt(this.HeaderCell);
            }

            /// <summary>
            /// Header合并的列数
            /// </summary>
            public int HeaderExpandedColumns { get; set; }
            /// <summary>
            /// Header合并文本
            /// </summary>
            public string HeaderExpandedColumnsText { get; set; }
            /// <summary>
            /// 列是否属于合并列
            /// </summary>
            public bool InHeaderExpandedColumns { get; set; }
        }

        public class DataGridViewColumnHeaderCellExt : DataGridViewColumnHeaderCell
        {
            public DataGridViewColumnHeaderCellExt(DataGridViewColumnHeaderCell oldCell)
            {
                this.ContextMenuStrip = oldCell.ContextMenuStrip;
                this.ErrorText = oldCell.ErrorText;
                this.Tag = oldCell.Tag;
                this.ToolTipText = oldCell.ToolTipText;
                this.Value = oldCell.Value;
                this.ValueType = oldCell.ValueType;

                if (oldCell.HasStyle)
                {
                    this.Style = oldCell.Style;
                }

            }

            public override object Clone()
            {
                return new DataGridViewColumnHeaderCellExt(this);
            }

            protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
            {
                if (!this.OwningColumn.Visible)
                {
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                    return;
                }

                if (this.OwningColumn is DataGridViewColumnExt)
                {
                    int expandWidth = 0;
                    DataGridViewColumnExt column = this.OwningColumn as DataGridViewColumnExt;

                    // 如果当前列是Expand的,那么计算HeaderCell合并后的Width
                    if (column.HeaderExpandedColumns > 1)
                    {
                        for (int i = column.Index; i < column.Index + column.HeaderExpandedColumns; i++)
                        {
                            if (i < this.OwningColumn.DataGridView.Columns.Count)
                            {
                                DataGridViewColumnExt column1 = this.OwningColumn.DataGridView.Columns[i] as DataGridViewColumnExt;

                                if (column1.Visible)
                                {
                                    expandWidth += column1.Width;
                                    column1.InHeaderExpandedColumns = true;
                                }
                            }
                        }

                        int left = cellBounds.Left;
                        int top = cellBounds.Top;
                        int height = cellBounds.Height;

                        Rectangle rect = new Rectangle(left, top, expandWidth + 1, cellBounds.Height / 2 + 1);

                        // 重绘合并部分的HeaderCell单元格背景
                        using (Brush backColorBrush = new SolidBrush(cellStyle.BackColor))
                        {
                            base.Paint(graphics, rect, rect, rowIndex, dataGridViewElementState, "", "", errorText, cellStyle, advancedBorderStyle, paintParts);
                        }

                        string headerText = column.HeaderExpandedColumnsText;
                        if (headerText != "")
                        {
                            //计算绘制字符串的位置
                            SizeF sf = graphics.MeasureString(headerText, cellStyle.Font);
                            float headerTextX = (expandWidth - sf.Width) / 2;
                            float headerTextY = (height / 2 - sf.Height) / 2;

                            // 重绘合并的HeaderText
                            using (Brush b = new SolidBrush(cellStyle.ForeColor))
                            {
                                graphics.DrawString(headerText, cellStyle.Font, b,
                                                     left + headerTextX,
                                                     top + headerTextY,
                                                     StringFormat.GenericDefault);
                            }

                        }
                    }

                    // 如果是其他HeaderCell,只要属于某个合并列,则高度只绘制一半
                    if (column.InHeaderExpandedColumns)
                    {
                        clipBounds = new Rectangle(clipBounds.X, clipBounds.Y + clipBounds.Height / 2, clipBounds.Width, clipBounds.Height / 2);
                        cellBounds = new Rectangle(cellBounds.X, cellBounds.Y + cellBounds.Height / 2, cellBounds.Width, cellBounds.Height / 2);
                    }

                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

                    // HeaderCell的表格线
                    using (Pen p = new Pen(this.DataGridView.GridColor))
                    {
                        graphics.DrawLine(p, new Point(cellBounds.X + cellBounds.Width - 1, cellBounds.Y - 1), new Point(cellBounds.X + cellBounds.Width - 1, cellBounds.Height));
                    }
                }
                else
                {
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                }
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值