winform中DataGridview数据绑定时格式化文本,如:将部分文字改成绿色,部分文字改成红色
在winform中,使用DataGridView时,想在某一列,值为“true”时,将这列颜色改变,并且将值也改变,需要用到如下方法:
private void gdvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 8 ) //哪一列
{
if (object.Equals(e.Value, "true"))
{
e.Value="成功";
e.CellStyle.ForeColor = Color.Green;
}
else
{
e.Value="失败";
e.CellStyle.ForeColor = Color.Red;
}
}
}