『转载』C# winform 中dataGridView的重绘(进度条,虚线,单元格合并等)
原文转载自:http://hi.baidu.com/suming/item/81e45b1ab9b4585f2a3e2243
最近比较浅的研究了一下dataGridView的重绘,发现里面还是有很多东西可以学习的,摘录如下(其实就是在dataGridView1_CellPainting写一些代码)
主要代码:
int top = ;
int left = ;
int height = ;
int width1 = ;
/// <summary>
/// 窗体的重绘事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
/** 表头处理*************************************/
if (e.RowIndex == -)
{
if (e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == )
{
top = e.CellBounds.Top;
left = e.CellBounds.Left - ;
height = e.CellBounds.Height;
width1 = e.CellBounds.Width;
Rectangle rect1 = new Rectangle(left, top, width1, e.CellBounds.Height);
//表头第一列
if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.Blue))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//表头第二列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.Orange))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//表头第三列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.Orange))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//表头第四列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.Orange))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//表头第五列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.Blue))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//表头第六列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.Blue))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
using (Pen gridLinePen = new Pen(Color.Green , ))
{
//绘出上线
e.Graphics.DrawLine(gridLinePen, left, top, left + width1, top);
//绘出下线
e.Graphics.DrawLine(gridLinePen, left, top + height, left + width1, top + height);
//绘出左线
e.Graphics.DrawLine(gridLinePen, left, top, left, top + height);
string columnValue = "";
//计算绘制字符串的位置
if (e.ColumnIndex == )
{
columnValue = "零件号";
}
else if (e.ColumnIndex == )
{
columnValue = "8:00--11:59";
}
else if (e.ColumnIndex == )
{
columnValue = "12:00--16:59";
}
else if (e.ColumnIndex == )
{
columnValue = "18:00--30:59";
}
else if (e.ColumnIndex == )
{
columnValue = "进度";
}
else if (e.ColumnIndex == )
{
columnValue = "进度率";
}
SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
float lstr = (width1 - sf.Width) / ;
//float rstr = sf.Height/2;
float rstr = (height - sf.Height) / ;
//画出文本框 if (columnValue != "")
{
e.Graphics.DrawString(columnValue, e.CellStyle.Font,
new SolidBrush(Color.White),
left + lstr,
top + rstr,
StringFormat.GenericDefault);
}
}
e.Handled = true;
} }
/** 内容处理*************************************/
else
{
if (e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == )
{
top = e.CellBounds.Top;
left = e.CellBounds.Left - ;
height = e.CellBounds.Height;
width1 = e.CellBounds.Width;
Rectangle rect1 = new Rectangle(left, top, width1, e.CellBounds.Height);
//内容第一列
if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.LightGreen))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//内容第二列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.White))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//内容第三列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.White))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//内容第四列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.White))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//内容第五列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.LightGreen))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
//内容第六列
else if (e.ColumnIndex == )
{
using (Brush backColorBrush = new SolidBrush(Color.LightGreen))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect1);
}
}
using (Pen gridLinePen = new Pen(Color.Green, ))
{
//绘出左线
e.Graphics.DrawLine(gridLinePen, left, top, left, top + height); }
//绘出进度率的线
if (e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == )
{
string str = e.Value.ToString();
int index =str.LastIndexOf ("/");
int widths = Convert.ToInt32(Convert.ToDouble(str.Substring(, index)) / Convert.ToDouble(str.Substring(index + )) * width1);
if (widths > width1)
{
widths = width1;
}
using (Pen gridLinePen = new Pen(Color.Blue, Convert.ToInt32(height / )))
{ e.Graphics.DrawLine(gridLinePen, left, top + height / + Convert.ToInt32(height / ), left + widths, top + height / + Convert.ToInt32(height /)); }
}
//画出上下线,中线和单元格的数据
using (Pen gridLinePen = new Pen(Color.Green , ))
{
float[] BlueValues = { , ,, };
gridLinePen.DashPattern = BlueValues;
//绘出上线
e.Graphics.DrawLine(gridLinePen, left, top, left + width1, top);
//绘出下线
e.Graphics.DrawLine(gridLinePen, left, top + height, left + width1, top + height);
if (e.ColumnIndex == || e.ColumnIndex == || e.ColumnIndex == )
{ //绘出中线
e.Graphics.DrawLine(gridLinePen, left, top + height / , left + width1, top + height / );
e.Graphics.DrawLine(gridLinePen, left + width1/, top + height / , left + width1/, top + height);
e.Graphics.DrawLine(gridLinePen, left + width1 / , top + height / , left + width1 / , top + height);
e.Graphics.DrawLine(gridLinePen, left + width1* / , top + height / , left + width1* / , top + height);
} string columnValue =e.Value.ToString () ;
SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
float lstr = (width1 - sf.Width) / ;
//float rstr = sf.Height/2;
float rstr = (height/ - sf.Height) / ;
//画出文本框数据
if (columnValue != "")
{
e.Graphics.DrawString(columnValue, e.CellStyle.Font,
new SolidBrush(Color.Black),
left + lstr,
top + rstr,
StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}
例图如下:

『转载』C# winform 中dataGridView的重绘(进度条,虚线,单元格合并等)的更多相关文章
- C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法
下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...
- 『转载』Debussy快速上手(Verdi相似)
『转载』Debussy快速上手(Verdi相似) Debussy 是NOVAS Software, Inc(思源科技)发展的HDL Debug & Analysis tool,这套软体主要不是 ...
- 『转载』hadoop2.x常用端口、定义方法及默认端口
『转载』hadoop2.x常用端口.定义方法及默认端口 1.问题导读 DataNode的http服务的端口.ipc服务的端口分别是哪个? NameNode的http服务的端口.ipc服务的端口分别是哪 ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色
winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...
- 【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小
上一篇:winform中dataGridView高度自适应填充完数据的高度 winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度 ...
- winform中dataGridView高度自适应填充完数据的高度
// winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...
- WinForm中DataGridView复制选中单元格内容解决方案
WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...
- DataGridView单元格合并
本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...
随机推荐
- 获取微信openID 的步骤
获取微信openid的步骤:1.进入-->判断openID是否为空: 空-->$url=urlencode("http://xxx/xxx.php");//回调链接 $ ...
- 【php】使用phpdbg来调试php程序
PHPDBG是一个PHP的SAPI模块,可以在不用修改代码和不影响性能的情况下控制PHP的运行环境 可以在PHP5.4和之上版本中使用.在PHP5.6和之上版本将内部集成 功能 单步调试 灵活的下断点 ...
- 【mysql】一个关于order by排序的问题
I have a table CREATE TABLE `tableMain` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value1` varchar(45) ...
- 问题解决——VS2010 将生成的文件复制到指定位置
我是从VC6直接过渡到VS2010的,VS2008没怎么用过.用VS2010的时候,每次生成dll后,手工把dll.lib..h文件复制到指定文件夹太麻烦了,所以着手写了这个. =========== ...
- JQ工具函数
在jQuery中,工具函数是指直接依附于jQuery对象,针对jQuery对象本身定义的方法,即全局性的,我们统称为工具函数,或Utilites函数 主要作用于:字符串.数组.对象 API:工具函数 ...
- Web性能优化之图片延迟加载
来源:微信公众号CodeL 对于一些图片多,页面长的网页来说,如果每次打开页面加载全部的网页内容,页面加载速度势必会受到影响,如果每次打开网页只将网页可视区域的内容加载给用户 ,将大大提高网页浏览速度 ...
- QEMU启动时插入tap虚拟网卡
1.利用brctl命令创建虚拟网桥br0 brctl addbr br0 ifconfig br0 up //上述两条命令分开执行会导致网络断开 2.将虚拟网桥br0与物理网卡eth0绑定 brctl ...
- JS练习题-Harshad numbers
在Codewars上面练习,现在到6级的题目了(数字越小越难) 这道题叫Harshad or Niven numbers, 原题http://www.codewars.com/kata/54a0689 ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- HDU 2487 Ugly Windows
递归求解,代码不太好看,是2013年7月写的 代码: #include<stdio.h> #include<iostream> #include<string.h> ...