『转载』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的重绘(进度条,虚线,单元格合并等)

上一篇:CUDA零内存拷贝 疑问考证


下一篇:我学习Python的经历