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 ...
随机推荐
- android 源码 中修改系统字体大小
在源码\android\frameworks\base\core\java\android\content\res \Configuration.java下有读取DEFAULT_FONTSCALE的值 ...
- 非官方的iOS设计指南
非官方的iOS设计指南 有时候为iOS设计app并不是一件简单的事,但是如果你能找到正确的最新的苹果设备信息,并按照正确的方向,那么为iOS设计app或许会变得简单容易些. 关于这些指南 这些指南描述 ...
- SurfaceView 和 TextureView
1.区别 The followings are two limitations of SurfaceView: You can not be animated, transformed and sca ...
- hdu 1281 棋盘游戏
http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- ado通用操作数据单元
DELPHI开发2层C/S数据库应用程序,许多人通过ADOQUERY或ADOTABLE直接操作数据库,其实这种方法虽然最为直接,但有其缺点:如果以后要将程序升级为3层C/S会非常困难.而通过像下面的通 ...
- class int
class int(object): """ int(x=0) -> integer int(x, base=10) -> integer Convert a ...
- cf754 B. Ilya and tic-tac-toe game
呵呵呵,这个题简直是一直在乱做,真是最近太弱了 #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL long l ...
- Jackson 高性能的JSON处理 ObjectMapper
http://blog.csdn.net/wangyang2698341/article/details/8223929 今天自行研究了下json ,感觉非常好用,经过测试比google的GSON快多 ...
- HTML第八天笔记
第一个知识点是关于伪类的,代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- MON166 User's Guide
MON166 is a debug monitor for C16x and ST10 user programs. It consists of: A configurable monitor pr ...