如何更改 DataGrid 中一行的字体颜色?
颜色取决于表中的条件。
我在 stackflow 上看到了这篇文章,但它仅适用于选定的行,并且该行将是另一种颜色,无论是否被选中。
您可以将事件处理程序添加到网格视图的Paint
.
如果您想做的不仅仅是颜色,我们已经采用了继承DataGridViewCell
和覆盖其 Paint 方法的路线,继承自DataGridViewColumn
以使用该单元格,然后在我们的网格视图中使用该列。
下面是被覆盖的方法,但事件处理程序看起来很相似。
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds,
int rowIndex, DataGridViewElementStates cellState, object value, object
formattedValue, string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts
paintParts)
{
if ((value as WhatEverType).WhatEverField == 9)
{
cellStyle.ForeColor = Color.CornflowerBlue;
}
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}