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 ...
随机推荐
- NativeInt
NativeInt ni = 100; //Wrong. Will issue an Error NativeInt ni = NativeInt(100); //Correct System::Ge ...
- HD2144Calculate S(n)
Problem Description Calculate S(n). S(n)=13+23 +33 +......+n3 . Input Each line will contain one int ...
- Ubuntu下Android编译环境的配置
从安装操作系统到编译程序结束,过程大致如下. 1. Ubuntu Linux操作系统安装软件包.使用 Ubuntu 14.04 Desktop系统.安装Linux系统到VMWare虚拟机上. 2. 完 ...
- jshint 安装使用
首先要安装nodjs, 参考另一篇文章: Ubuntu 编译安装node.js 然后运行 npm install jshint -g 之后在要扫描的目录下运行命令 jshint . >> ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- RS232串口用事件接受数据(一问一答)
private void button1_Click(object sender, EventArgs e) { serialPort1.Open(); serialPort1.DataReceive ...
- 转载:页面加载swf插件:swfobject
转自:http://www.cnblogs.com/analyzer/articles/1299592.html 我一直都在用SWFObject 插入flash,好处多多,代码简洁,不会出现微软的“单 ...
- opennebula 创建模板【配置集群、配置VNC、配置RAW、配置SSH】
{ "vmtemplate": { "NAME": "bbbb", "MEMORY": "512", ...
- 用Java实现菱形的打印输出
import java.util.Scanner; public class dengyao2 { public dengyao2() { super(); } public static void ...
- 关闭IE窗口
$a=(New-Object -comObject Shell.Application).Windows() ($a|?{$_.locationname -eq "人力与人才信息管理系统&q ...