您的位置:首页 > 其它

winform中改变DataGridView中符合条件的字体的颜色和列的颜色 .

2013-11-09 07:52 344 查看
/article/9938422.html

只需要使用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

{

}

}

效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: