开发者

DataGridView special coloring - which way is better styles or cellformatting event?

开发者 https://www.devze.com 2023-03-08 09:10 出处:网络
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.

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

精彩评论

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