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 == - && e.RowIndex == -)
{
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(-, -));
e.Graphics.DrawRectangle(Pens.Gray, border);
}
e.PaintContent(e.CellBounds);
e.Handled = true;
}
else if (e.RowIndex == -)
{
//标题行
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(-, -));
e.Graphics.DrawRectangle(Pens.Gray, border);
}
e.PaintContent(e.CellBounds);
e.Handled = true;
}
else if (e.ColumnIndex == -)
{
//标题列
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(-, -));
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 ? : ; //水平滚动条高
int rowheaderswidth = dataGridView.RowHeadersVisible ? dataGridView.RowHeadersWidth : ;
//行标题宽度
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 = ;
lblParent.Top = height - scrollbarheight - - ;
lblParent.Height = this.dataGridViewEx1.ColumnHeadersHeight -;
//lblParent.Width = width + rowheaderswidth -1;
lblParent.Width = this.dataGridViewEx1.Width - ;
dataGridView.Controls.Add(lblParent);
for (int i = ; 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 / ) - + ;
if (column.Name == "clmPrice")
{
lblChild.Text = "合计:00.00";
lblChild.Left = length + ((int)column.Width / ) - ;
lblChild.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)()));
lblChild.ForeColor = System.Drawing.Color.Red;
}
length += column.Width;
lblChild.Top = ;
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 = ;
for (int i = ; 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();
}
}
上一篇:windows server下设置远程会话自动注销


下一篇:10分钟学会在windows/Linux下设置ASP.Net Core开发环境并部署应用