C# 自訂重繪DataGridView

來源:互聯網
上載者:User

標籤:des   datagridview   style   class   blog   code   

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();            }        }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.