http://blog.youkuaiyun.com/wang4978/article/details/4585251
只需要使用DataGridView的CellPainting事件。代码如下:
private void dgvUsers_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex < 0)
return;
DataGridViewRow dgr = dgvUsers.Rows[e.RowIndex];
try
{
if (e.ColumnIndex == 3)//定位到第3列,如不定位到具体的哪列,则整行的颜色改变
{
if (dgr.Cells["Column4"].Value.ToString() == "停用")//dgr.Cells["Column4"]为需要判断的列Name,而不是HeadText
{
e.CellStyle.ForeColor = Color.Red;//将前景色设置为红色,即字体颜色设置为红色
e.CellStyle.BackColor = Color.Green;//将背景色设置为绿色,即列的颜色设置为红色
}
}
}
catch
{
}
}
效果图:

本文介绍了一种在DataGridView中根据特定条件为单元格着色的方法。通过使用CellPainting事件,可以根据某列的值来改变另一列的字体颜色和背景色。
1143

被折叠的 条评论
为什么被折叠?



