DataGridView 中单元格显示不一样颜色方法
2014-10-23 14:49
281 查看
1、DataGridView 中添加CellPainting事件方法
2、在CellPainting函数内部进行重绘
代码如下:
private void dgvSteps_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex > -1 && e.RowIndex >= 0)//要进行重绘的单元格
{
Graphics gpcEventArgs = e.Graphics;
Color clrBack = e.CellStyle.BackColor;
Font fntText = e.CellStyle.Font;//获取单元格字体
//先使用北京颜色重画一遍背景
gpcEventArgs.FillRectangle(new SolidBrush(clrBack), e.CellBounds);
//设置字体的颜色
Color oneFore = System.Drawing.Color.Black;
Color secFore = System.Drawing.Color.Red;
string strFirstLine = "黑色内容";
string strSecondLine = "红色内容";
Size sizText = TextRenderer.MeasureText(e.Graphics, strFirstLine, fntText);
int intX = e.CellBounds.Left + e.CellStyle.Padding.Left;
int intY = e.CellBounds.Top + e.CellStyle.Padding.Top;
int intWidth = e.CellBounds.Width - (e.CellStyle.Padding.Left + e.CellStyle.Padding.Right);
int intHeight = sizText.Height + (e.CellStyle.Padding.Top + e.CellStyle.Padding.Bottom);
//第一行
TextRenderer.DrawText(e.Graphics, strFirstLine, fntText, new Rectangle(intX, intY, intWidth, intHeight),
oneFore, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
//另起一行
intY = intY + intHeight - 1;
TextRenderer.DrawText(e.Graphics, strSecondLine, fntText, new Rectangle(intX, intY, intWidth, intHeight),
secFore, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
////在同一行后面画
//intX = sizText.Width - 1;//在这行后面添加
//if (intX < e.CellBounds.Width)//能够显示的时候才进行绘制
//{
// intWidth = e.CellBounds.Width - intX;
// TextRenderer.DrawText(e.Graphics, strSecondLine, fntText, new Rectangle(intX, intY, intWidth, intHeight),
// secFore, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
//}
e.Handled = true;
}
}
2、在CellPainting函数内部进行重绘
代码如下:
private void dgvSteps_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex > -1 && e.RowIndex >= 0)//要进行重绘的单元格
{
Graphics gpcEventArgs = e.Graphics;
Color clrBack = e.CellStyle.BackColor;
Font fntText = e.CellStyle.Font;//获取单元格字体
//先使用北京颜色重画一遍背景
gpcEventArgs.FillRectangle(new SolidBrush(clrBack), e.CellBounds);
//设置字体的颜色
Color oneFore = System.Drawing.Color.Black;
Color secFore = System.Drawing.Color.Red;
string strFirstLine = "黑色内容";
string strSecondLine = "红色内容";
Size sizText = TextRenderer.MeasureText(e.Graphics, strFirstLine, fntText);
int intX = e.CellBounds.Left + e.CellStyle.Padding.Left;
int intY = e.CellBounds.Top + e.CellStyle.Padding.Top;
int intWidth = e.CellBounds.Width - (e.CellStyle.Padding.Left + e.CellStyle.Padding.Right);
int intHeight = sizText.Height + (e.CellStyle.Padding.Top + e.CellStyle.Padding.Bottom);
//第一行
TextRenderer.DrawText(e.Graphics, strFirstLine, fntText, new Rectangle(intX, intY, intWidth, intHeight),
oneFore, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
//另起一行
intY = intY + intHeight - 1;
TextRenderer.DrawText(e.Graphics, strSecondLine, fntText, new Rectangle(intX, intY, intWidth, intHeight),
secFore, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
////在同一行后面画
//intX = sizText.Width - 1;//在这行后面添加
//if (intX < e.CellBounds.Width)//能够显示的时候才进行绘制
//{
// intWidth = e.CellBounds.Width - intX;
// TextRenderer.DrawText(e.Graphics, strSecondLine, fntText, new Rectangle(intX, intY, intWidth, intHeight),
// secFore, TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.EndEllipsis);
//}
e.Handled = true;
}
}
相关文章推荐
- [唐胡璐] - DataGridView单元格显示多行的设置方法
- 各行显示 颜色不一样方法(struts2)标签用法!!!
- DataGridView单元格显示多行的设置方法
- [唐胡璐] - DataGridView单元格显示多行的设置方法
- C#中datagridview使用tooltip控件显示单元格内容的方法
- dataGridView操作数据(增加 读取 绑定 获取选择的单元格 选中整行单元格时颜色变换 更新数据 锁定列的位置 添加行 列头的方法)
- C#中datagridview使用tooltip控件显示单元格内容与datagridview自带的tooltip显示单元格内容的方法
- CSS样式定义table隔行背景颜色显示不一样(IE8及以上,其他浏览器不支持)
- VB.NET中根据DataGridView中显示的数据条件数据行显示不同的颜色
- IE不能显示网页的背景颜色的解决方法
- dataGridView 显示数据 定位到首记录 定位到未记录的方法
- [原创]微软BI专题-一种动态显示Matrix中Total背景颜色的方法
- [VB.NET]datagridview的单元格的值为0的时候想显示空,但是提交的时候,空的想转换为0,怎么做?
- DataGridView单元格中播放gif动画的变通方法
- GridView判断使文字显示不同的颜色的两种方法:1前右判断;2.后台判断
- EXTJS GRID 改变单元格背景颜色的方法
- C# 中 datagridview控件 在行标题显示行号的方法
- DataGridView的paint方法来绘制一行”假”Row来显示(转)
- 一:从实例入手之设置设置颜色交替显示的GridView(方法一)
- DataGridView 使用comboBox列,只显示自定义列的方法(winForm)