原文转载自: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的重绘(进度条,虚线,单元格合并等)的更多相关文章

  1. C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

    下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...

  2. 『转载』Debussy快速上手(Verdi相似)

    『转载』Debussy快速上手(Verdi相似) Debussy 是NOVAS Software, Inc(思源科技)发展的HDL Debug & Analysis tool,这套软体主要不是 ...

  3. 『转载』hadoop2.x常用端口、定义方法及默认端口

    『转载』hadoop2.x常用端口.定义方法及默认端口 1.问题导读 DataNode的http服务的端口.ipc服务的端口分别是哪个? NameNode的http服务的端口.ipc服务的端口分别是哪 ...

  4. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

  5. winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色

    winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...

  6. 【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小

    上一篇:winform中dataGridView高度自适应填充完数据的高度 winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度 ...

  7. winform中dataGridView高度自适应填充完数据的高度

    // winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...

  8. WinForm中DataGridView复制选中单元格内容解决方案

    WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...

  9. DataGridView单元格合并

    本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...

随机推荐

  1. Java解决题目:有一对兔子,从出生第三个月起每个月都生一对兔子,小兔子长到第三个月后,每个月又生一对兔子。。。

    题目:有一对兔子,从出生第三个月起每个月都生一对兔子,小兔子长到第三个月后,每个月又生一对兔子,假如兔子都不死,问M个月时兔子的数量,M为键盘读入的正整数.(请用Java语言作答) 样例输入: 3 样 ...

  2. grub条目示例

    https://wiki.archlinux.org/index.php/GRUB#Install_to_disk /boot/grub/menu.lst default=0 timeout=5 ti ...

  3. SQL2008 提示评估期已过的解决方法

    提示窗口: 解决步骤: 第一步:进入SQL2008配置工具中的安装中心 第二步:再进入维护界面,选择版本升级 第三步:进入产品密钥,输入密钥 第四步:一直点下一步,直到升级完毕. SQL Server ...

  4. linux alarm函数解除read write等函数的阻塞

    看到apue的第十章,说到alarm,pause可以实现sleep,可以让某些一直阻塞的函数超时,例如read,write.代码如下: static void sig_alrm(int signo) ...

  5. git一些常用设置

    用法:git config [选项] 配置文件位置    --global              使用全局配置文件    --system              使用系统级配置文件    -- ...

  6. HTML常用文本元素

    HTML是超文本标记语言,它提供网页的具体内容,包括文本.表单.图像.表格.链接.多媒体.列表等.其中文本是我们遇到的最多的展示内容.正确的使用文本标签,会使页面具有语义化,也有利于SEO. 文本标签 ...

  7. Java实现事件机制

    java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...

  8. AppScan Source V8.8 中弃用的功能

    从 AppScan Source V8.8 开始,不再支持以下操作系统: Microsoft Windows XP Microsoft Windows Server 2003,所有版本和修订版   此 ...

  9. java 21 - 12 随机访问流(不属于IO流)

    随机访问流: RandomAccessFile类不属于流,是Object类的子类. 但它融合了InputStream和OutputStream的功能. 支持对文件的随机访问读取和写入. public ...

  10. javascript之享元模式

    实现享元模式的一般步骤: 1.将所有外在数据从目标类中剥离.具体做法是尽可能多的删除该类的属性,所删除的应该是那种因实例而异的属性.构造函数的参数也要这样处理,这些参数应该被添加到该类的各个方法. 这 ...