原文转载自: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. 如何删除TFS的Team Project

    我们可以使用Visual Studio或Web新建一个TeamProject,但是删除时却没有一个合适的图形界面删除我们不想要的Team Project,所以此时就可以使用命令TFSDeletePro ...

  2. 限制input输入类型(多种方法实现)

    1.只能输入和粘贴汉字 <input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste= ...

  3. html的input输入框边框

    1.输入框边框完全隐藏<input type="text" style="outline:none;border:0" /> 2.input去边框立 ...

  4. loop指令

    loop系列的指令有:loop,loope/loopz,loopne/loopnz,它们都是借助于ECX寄存器作为计数来实现循环,每轮循环先ecx自动减1,再来判断ecx值,ecx的自减不会影响OF和 ...

  5. Hadoop blocks

    一In cases where the last record in a block is incomplete, the input split includes location informat ...

  6. [转]Ionic + AngularJS angular-translate 国际化本地化解决方案

    本文转自:http://www.cnblogs.com/maoyazhi/p/4332362.html 欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术 ...

  7. 【对比分析八】null和undefined的区别

    (1).  null是一个表示"无"的对象,转为数值时为0  undefined是一个表示"无"的原始值,转为数值时为NaN (2).  当声明的变量还未被初始 ...

  8. Chorme中启动阿里旺旺误点取消启动并记住选择,如何更改。

    今天在Chorme中启动阿里旺旺误点取消启动并记住选择,然后如何也点不开了.从网上找到了一种解决方法: 找到路径   C:\Users\\AppData\Local\Google\Chrome\Use ...

  9. 【问题&解决】试用版SQL Server 2008 R2 提示评估期已过,数据库不能访问解决办法

    因为以前一直是试用版,重启服务器之后,突然数据库不能访问,提示评估期已过,都快吓死了.还好找到了解决办法特copy解决步骤如下: (笔者用的是企业版: R88PF-GMCFT-KM2KR-4R7GB- ...

  10. 给Testerhome测试小道消息做个硬广告

    测试小道消息在荔枝FM上的粉丝马上就要超过1k了.还差17个我们就能够开通社区了.欢迎大家都来收听测试小道消息哈--更多精彩内容还希望大家下载荔枝fm.关注FM245329. 如果你还不了解测试小道消 ...