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 ...
随机推荐
- Orion Network Performance Monitor 软件在网络管理中的应用
Orion Network Performance Monitor 软件在网络管理中的应用 Orion Network Performance Monitor是完全的带宽性能和故障管理软件 ...
- 实现系统函数time,获取当前时间与UTC的间隔
因种种原因,最近很少上cnblogs了.刚写了一个实现time的函数,可以通过该函数获取当前时间与1970年1月1日 0时0分0秒的差值,精确到秒,可以用在某些没有时候使用time不正确而不得不调用硬 ...
- webrtc--AudioProcessing的使用
1.AudioProcessing的实例化和配置: AudioProcessing* apm = AudioProcessing::Create(0); apm->level_estimator ...
- Prefabs
[Prefabs] A Prefab is a type of asset -- a reusable GameObject stored in Project View. Prefabs can b ...
- netdata linux环境下的安装
据说netdata监控很个性化,采用的显示方式也很漂亮,就来尝试安装.百度搜索到的安装教程的斑斑是1.0.顺藤摸瓜去wiki看了看,已经更新为1.4了,果断走起: 下载地址::https://gith ...
- c# abstract抽象类与继承类子类的构造函数_base
http://blog.itpub.net/9240380/viewspace-718054/ http://blog.163.com/cloud_thegreat/blog/static/10367 ...
- C# 支付宝接口
生成URL链接 1using System; 2using System.Data; 3using System.Configuration; 4using System.Collectio ...
- 检测一个DOM对象是否为空
我们时常要检测一个DOM对象是否为空. var $jObject = $('#btn'); alert($jObject ); 我们会发现,$jObject 永远不会为空.为什么呢?$ 方法查找对象, ...
- XGrid绑定(转)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Fo ...
- Android开发之SQLite的使用方法
前言 SQLite是一种轻量级的小型数据库,虽然比较小,但是功能相对比较完善,一些常见的数据库基本功能也具有,在现在的嵌入式系统中使用该数据库的比较多,因为它占用系统资源很少.Android系统中也不 ...