DataGridView special coloring - which way is better styles or cellformatting event?
I'm wondering for efficiency which is a better way to do the datagridview cell coloring?
You can use styles set on the grid at design time. I don't use these often though for some reason.
or
you can handle the cel开发者_开发知识库lformatting event of every single cell in the grid and do comparisions.
I do not know how the styles check and apply the style but if it makes less calls than a cell formatting event for every single cell it would seem that it is better. I wasn't sure so I figured I'd ask here.
The CellFormatting
event is indeed an expensive option, as it is called for every visible cell each time it is painted: setting a style is easier I find if you do it in code:
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle
{
Alignment = DataGridViewContentAlignment.MiddleLeft,
BackColor = Color.White,
ForeColor = Color.Black,
SelectionBackColor = Color.FromArgb(224, 224, 224),
SelectionForeColor = Color.Black,
WrapMode = DataGridViewTriState.False,
NullValue = string.Empty
};
myDGV.DefaultCellStyle = cellStyle;
myDGV.ColumnHeadersDefaultCellStyle = cellStyle;
精彩评论