开发者

Font color of a row in DataGrid

H开发者_Python百科ow can I change the font color of a row in DataGrid?

The color depends on a condition in the table.

I saw this post here on stackflow but it works only for the selected lines, and that the line would be another color, regardless of being selected or not.


You could just add an event handler to the grid view's Paint.

If you want to do more than just the colour, we have gone the route of inheriting from DataGridViewCell and overriding its Paint method, inheriting from DataGridViewColumn to use that cell, then use that column in our grid view.

Below is the overridden method, but the event hander would look similar.

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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜