DataGrid中一行的字体颜色

时间:2011-03-29 14:44:29

标签: c# datagrid compact-framework row windows-mobile-6.5

如何在DataGrid中更改行的字体颜色?

颜色取决于表格中的条件。

我在堆栈流上看到了this,但它仅适用于选定的行,并且该行将是另一种颜色,无论是否被选中。

1 个答案:

答案 0 :(得分:0)

您只需将一个事件处理程序添加到网格视图的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);
        }
相关问题