『转载』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单元格合并源码 也可以参考: ...
随机推荐
- Struts中的OGNL和EL表达式笔记
Struts中的OGNL和EL表达式笔记 OGNL(Object-Graph Navigation Language),可以方便的操作对象属性的表达式语言. 1.#符号的用途 一般有三种方式: 1.1 ...
- Eclipse 一直提示 loading descriptor for 的解决方法
启动eclipse之后,进行相关操作时,弹出界面,提示:loading descriptor for xxx 解决方法: 在Eclipse左侧的Project Explorer 最右上角有一个小钮,鼠 ...
- 大数据架构-使用HBase和Solr将存储与索引放在不同的机器上
大数据架构-使用HBase和Solr将存储与索引放在不同的机器上 摘要:HBase可以通过协处理器Coprocessor的方式向Solr发出请求,Solr对于接收到的数据可以做相关的同步:增.删.改索 ...
- Ubuntu,QT5连接MySQL
用QT连接MySQL需要共享库 libqsqlmysql.so的驱动,路径在plugin/sqldrivers目录下,乍看已经可用了,其实不然. 用ldd命令分析一下,libmysqlclient_r ...
- JSON实战案例--使用JSON进行数据交换实例
内容: 1.访问javaScript对象的属性 2.访问Javacript对象数组 3.JSON字符串转换成JavaScript对象 4 .Java对象转换成JSON字符串 5 .使用JSON完成级联 ...
- linux移植问题汇总(一)
linux移植问题汇总(一) 在此记录移植linux过程中出现的问题以及解决方法. 项目GitHub地址 linux3.0.80:https://github.com/numbqq/linux-3.0 ...
- SQL中case语句的两种方式
- DirectX API 编程起步 #02 窗口的诞生
在这篇文章里我们先用 windows API 制作一个窗口出来,以后再用 DirectX API 渲染的东西就会显示在这里,控制台那黑白的画面肯定是没法用的. 每次的代码都会更新到Github 首先贴 ...
- OpenGL 学习笔记 01 环境配置
以下教程仅适用于Mac下的Xcode编程环境!其他的我也不会搞. 推荐教程:opengl-tutorial 本项目Github网址 OpenGL太可怕了...必需得把学的记下来,不然绝壁 ...
- Properties集合的练习
1.更改文件中的数据,特定键的值: 需求:我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么. 请写一个程序判断是否有"lisi"这样的键存在,如 ...