代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
DataGirdView 自定义进度条列
作者:
scottckt
/ 发布于
2011/6/22
/
596
<div> 1、添加引用 <div style="width: 900px" class="cnblogs_code"> <div><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Drawing; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.ComponentModel;</span></div> </div> 2、自定义DataGridViewcolumn <div style="width: 900px" class="cnblogs_code"> <div><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 带进度列。单元格内容格式为:值/总值 Eric </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)">[Description(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">带进度列。单元格内容格式为:值/总值</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">)] </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> DataGridViewProcessColumn : DataGridViewColumn { </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> DataGridViewProcessColumn() : </span><span style="color: rgb(0,0,255)">base</span><span style="color: rgb(0,0,0)">(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> DataGridViewProcessCell()) { } }</span></div> </div> 3、自定义DataGridViewCell <div style="width: 900px" class="cnblogs_code"> <div><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 带进度单元格。单元格内容格式为:值/总值 Eric </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)">[Description(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">带进度单元格。单元格内容格式为:值/总值</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">)] </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> DataGridViewProcessCell : DataGridViewCell { </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 进度条景色 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,255)">protected</span><span style="color: rgb(0,0,0)"> Color _ProcessColor </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Color.FromArgb(</span><span style="color: rgb(128,0,128)">64</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">192</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">192</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> DataGridViewProcessCell() { } </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> DataGridViewProcessCell(Color processBackColor) : </span><span style="color: rgb(0,0,255)">base</span><span style="color: rgb(0,0,0)">() { _ProcessColor </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> processBackColor; } </span><span style="color: rgb(0,0,255)">protected</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">override</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> rowIndex, DataGridViewElementStates cellState, </span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> value, </span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> formattedValue, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> (SolidBrush backBrush </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> SolidBrush(cellStyle.BackColor)) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">维制单元格背景</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">graphics.FillRectangle(backBrush, cellBounds); } </span><span style="color: rgb(0,0,255)">base</span><span style="color: rgb(0,0,0)">.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); </span><span style="color: rgb(0,0,255)">float</span><span style="color: rgb(0,0,0)"> percent </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> 0F; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (value </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] resultAndTotal </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value.ToString().Split(</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">/</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (resultAndTotal.Count() </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">float</span><span style="color: rgb(0,0,0)"> result </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">float</span><span style="color: rgb(0,0,0)"> total </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,255)">float</span><span style="color: rgb(0,0,0)">.TryParse(resultAndTotal[</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">], </span><span style="color: rgb(0,0,255)">out</span><span style="color: rgb(0,0,0)"> result)) { percent </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,255)">float</span><span style="color: rgb(0,0,0)">.TryParse(resultAndTotal[</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">], </span><span style="color: rgb(0,0,255)">out</span><span style="color: rgb(0,0,0)"> total)) { percent </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)"> { percent </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> result </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> total; } } } </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> (SolidBrush foreBrush </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> SolidBrush(_ProcessColor)) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">绘制进度图</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">graphics.FillRectangle(foreBrush, cellBounds.X, cellBounds.Y </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> cellBounds.Height </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">4</span><span style="color: rgb(0,0,0)">, cellBounds.Width </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> percent, cellBounds.Height </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">); } </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> (SolidBrush residueBrush </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> SolidBrush(Color.LightGray)) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">绘进度条余下部分</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">graphics.FillRectangle(residueBrush, cellBounds.X </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> cellBounds.Width </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> percent, cellBounds.Y </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> cellBounds.Height </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">4</span><span style="color: rgb(0,0,0)">, cellBounds.Width </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> percent), cellBounds.Height </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">); } </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> cellText </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value.ToString(); SizeF sf </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> graphics.MeasureString(cellText, cellStyle.Font); </span><span style="color: rgb(0,0,255)">float</span><span style="color: rgb(0,0,0)"> x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> cellBounds.X </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> (cellBounds.Width </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> sf.Width) </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> 2f; </span><span style="color: rgb(0,0,255)">float</span><span style="color: rgb(0,0,0)"> y </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> cellBounds.Y </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> (cellBounds.Height </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> sf.Height) </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> 2f; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">维制单元格文本</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">graphics.DrawString(cellText, cellStyle.Font, </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> SolidBrush(cellStyle.ForeColor), x, y); } }</span></div> </div> 运行:如果某Cell的value为:1/20。则我们看到结果将会与图中最后一条结果相同。 </div>
试试其它关键字
进度条
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
scottckt
贡献的其它代码
(
10
)
.
CommandArgument绑定多个值
.
js对Url进行utf8编码与解码
.
对Url进行utf8编码与解码
.
正则取出img中src
.
js操作FCKeditor方法
.
sql中escape用法
.
System.DBNull的用法
.
DataGridView控件隔行换色
.
得到应用程序所在路径
.
DataGirdView 自定义进度条列
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3