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();
}
}
C# 自定义重绘DataGridView的更多相关文章
- C# 自定义重绘TextBox
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 重绘DataGridView标头
最近突然想在DataGridView标头放置一个CheckBox,我就想着重写下DataGridViewColumnHeaderCell抱着试试的心态结果真的是可以的下面是源码:(如果有看不懂的可以加 ...
- 重绘DataGridView的DataGridViewCheckBoxCell控件
最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有 的是不支持的然后我就想着重写个 DataGridViewCheckB ...
- C# 自定义重绘TabControl
using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runti ...
- Windows开发进阶之VC++中如何实现对话框的界面重绘
技术:Windows 系统+Visual studio 2008 概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...
- 『转载』C# winform 中dataGridView的重绘(进度条,虚线,单元格合并等)
原文转载自:http://hi.baidu.com/suming/item/81e45b1ab9b4585f2a3e2243 最近比较浅的研究了一下dataGridView的重绘,发现里面还是有很多东 ...
- [DForm]我也来做自定义Winform之另类标题栏重绘
据说得有楔子 按照惯例,先来几张样例图(注:为了展示窗口阴影效果,截图范围向外扩展了些,各位凭想象吧). 还要来个序 其实,很多年没写过Winform了,前端时间在 ...
- C# DataGridView 更改类型 重绘
DataGridView 更改类型 需要用到重绘 DataGridViewTextBoxColumn aa01 = new DataGridViewTextBoxColumn(); aa00.Da ...
- c#winform自定义窗体,重绘标题栏,自定义控件学习
c#winform自定义窗体,重绘标题栏 虽然现在都在说winform窗体太丑了,但是我也能尽量让桌面应用程序漂亮那么一点点话不多说,先上图 重绘标题栏先将原生窗体设置成无边框,FormBoderSt ...
随机推荐
- var隐式类型
var dogName = "ruiky"; 1.[编译器]会在编译时自动根据值的类型推断这个变量的类型: 2.变量类型不可更改:因为声明的时候已经确定类型了. 3.可 ...
- 如果Apache Spark集群中没有分布式系统,则会?
若当连接到Spark的master之后,若集群中没有分布式文件系统,Spark会在集群中每一台机器上加载数据,所以要确保Spark集群中每个节点上都有完整数据. 通常可以选择把数据放到HDFS.S3或 ...
- C++11之使用或禁用对象的默认函数
[C++11之使用或禁用对象的默认函数] C++11 允许显式地表明采用或拒用编译器提供的内置函数.例如要求类型带有默认构造函数,可以用以下的语法: 另一方面,也可以禁止编译器自动产生某些函数.如下面 ...
- ilog
PCISV7-VHL [2015-11-13 13:51:36,038]>>>INFO>>>[ com.isoftstone.pcis.policy.app.pla ...
- python 应用xml.dom.minidom读xml
xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...
- apache配置虚拟主机后,启动速度慢
apache配置虚拟主机后,启动速度慢且提示“the requested operation has failed” 可以通过在cmd下启动,来查找问题(命令中的“apache2.2”,是服务名,根据 ...
- C#扫描仪编程、条形码识别编程资料
扫描仪编程资料:http://www.cnblogs.com/wubh/archive/2011/11/07/2239178.html 图片条形码识别资料:http://www.codeproject ...
- VS 2010下单元测试
1.创建单元测试项目 2.创建完成后,新建项目会自动添加“Microsoft.VisualStudio.QualityTools.UnitTestFramework”的引用,该引用用于单元 ...
- Apache实现动态虚拟主机
经常在开发中为Apache web server添加虚拟主机 方便多个项目的 同时运营,但是每次增加新的项目时都得重新配置增加VirtualHost:虚拟主机 部分,时间久了VirtualHo ...
- RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡
原文地址:http://www.cnblogs.com/mchina/archive/2012/05/23/2514728.html 一.简介 LVS是Linux Virtual Server的简写, ...