首页 > 代码库 > C# 自定义重绘DataGridView

C# 自定义重绘DataGridView

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Drawing.Drawing2D;

namespace ControlExs.ControlExs.DataGridView
{
    /// <summary>
    /// 扩展DataGrid控件
    /// </summary>
    [ToolboxBitmap(typeof(DataGrid))]
    public partial class DataGridViewEx : System.Windows.Forms.DataGridView
    {
        private Color headersColor;

        [Description("获取或设置DataGridView 表头的颜色的颜色")]
        [DefaultValue(typeof(Color))]
        public Color HeadersColor
        {
            get { return headersColor; }
            set { headersColor = value; }
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            base.OnCellPainting(e);
            if (e.ColumnIndex == -1 && e.RowIndex == -1)
            {
                using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
                    Color.White, LinearGradientMode.ForwardDiagonal))
                {
                    e.Graphics.FillRectangle(brush, e.CellBounds);
                    Rectangle border = e.CellBounds;
                    border.Offset(new Point(-1, -1));
                    e.Graphics.DrawRectangle(Pens.Gray, border);
                }
                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }
            else if (e.RowIndex == -1)
            {
                //标题行
                using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
                    Color.White, LinearGradientMode.Vertical))
                {
                    e.Graphics.FillRectangle(brush, e.CellBounds);
                    Rectangle border = e.CellBounds;
                    border.Offset(new Point(-1, -1));
                    e.Graphics.DrawRectangle(Pens.Gray, border);
                }
                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }
            else if (e.ColumnIndex == -1)
            {
                //标题列
                using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
                    Color.White, LinearGradientMode.Horizontal))
                {
                    e.Graphics.FillRectangle(brush, e.CellBounds);
                    Rectangle border = e.CellBounds;
                    border.Offset(new Point(-1, -1));
                    e.Graphics.DrawRectangle(Pens.Gray, border);
                }
                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }

        }
    }
}

在 DataGridView 底部加入统计行

/// <summary> 
        /// 添加统计行
        /// </summary>  
        /// <param name="dataGridView"></param>  
        public void AddLable(DataGridView dataGridView)
        {
            Label lblParent, lblChild;
            DataGridViewColumn column;
            int height = dataGridView.Height;
            int width = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
            int scrollbarheight = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) > dataGridView.Width ? 16 : 0;
            
            //水平滚动条高
            int rowheaderswidth = dataGridView.RowHeadersVisible ? dataGridView.RowHeadersWidth : 0;
            //行标题宽度
            int length = rowheaderswidth;
            //
            if (dataGridView.Controls[dataGridView.Name + "_footer"] != null)
            {
                dataGridView.Controls.Remove(dataGridView.Controls[dataGridView.Name + "____"]);
            }
            lblParent = new Label();
            lblParent.Name = dataGridView.Name + "_footer";
            lblParent.BackColor = Color.LightGray;
            lblParent.Left = 1;
            lblParent.Top = height - scrollbarheight - 23 - 1;
            lblParent.Height = this.dataGridViewEx1.ColumnHeadersHeight -2;
            //lblParent.Width = width + rowheaderswidth -1;
            lblParent.Width = this.dataGridViewEx1.Width - 2;
            dataGridView.Controls.Add(lblParent);
            for (int i = 0; i < dataGridView.Columns.Count; i++)
            {
                column = dataGridView.Columns[i];
                if (column.Visible)
                {
                    lblChild = new Label();
                    lblChild.Name = column.Name + "_footer";
                    lblChild.BackColor = Color.Transparent;
                    lblChild.AutoSize = true;
                    lblChild.Left = length + ((int)column.Width / 2) - 10 + 1;
                    if (column.Name == "clmPrice")
                    {
                        lblChild.Text = "合计:00.00";
                        lblChild.Left = length + ((int)column.Width / 2) - 60 ;
                        lblChild.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        lblChild.ForeColor = System.Drawing.Color.Red;
                    }
                    length += column.Width;
                    lblChild.Top = 5;
                    lblParent.Controls.Add(lblChild);
                }
            }
        }

        /// <summary> 
        /// 获取列总和
        /// </summary>  
        /// <param name="dataGridView"></param> 
        /// <param name="columnName"></param>  
        public void SetSUM(DataGridView dataGridView, string columnName)
        {
            if (dataGridView.Controls[dataGridView.Name + "_footer"] != null)
            {
                decimal sum = 0;
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    try
                    {
                        if (dataGridView.Rows[i].Cells[columnName].Value != null)
                        {
                            sum += decimal.Parse(dataGridView.Rows[i].Cells[columnName].Value.ToString());
                        }
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                }
                dataGridView.Controls[dataGridView.Name + "_footer"].Controls[columnName + "_footer"].Text = sum.ToString();
            }
        }