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