C#datagridview.单元格背景色为空,尽管DefaultCellStyle [英] C# datagridview. Cell backcolor is empty dispite of DefaultCellStyle

查看:47
本文介绍了C#datagridview.单元格背景色为空,尽管DefaultCellStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下方式将datagridview中的单元格背景色设置为黄色

I set the cell background color in a datagridview to yellow via

`grid.DefaultCellStyle.BackColor = Color.Yellow;`

我看到黄色的单元格,但是单元格的背景色也应该不是黄色吗?我得到的是"Color [Empty]",而不是黄色.如果尝试以下代码,为什么颜色不是黄色? grid.CellClick + =新的DataGridViewCellEventHandler((eventsource,cellevent)=>{int rowIndex = cellevent.RowIndex;int colIndex = cellevent.ColumnIndex;MessageBox.Show("color:" + grid.Rows [rowIndex] .Cells [colIndex] .Style.BackColor);});

I see the yellow cells but should the back color property of the cell not also be yellow? I get an "Color[Empty]" instead of color yellow. Why is the color not Yellow if I try the following code? grid.CellClick += new DataGridViewCellEventHandler( (eventsource, cellevent) => { int rowIndex = cellevent.RowIndex; int colIndex = cellevent.ColumnIndex; MessageBox.Show(" color: "+ grid.Rows[rowIndex].Cells[colIndex].Style.BackColor); });

推荐答案

单元格的背景色也应该不是黄色吗?

should the back color property of the cell not also be yellow?

不,不应该.

请注意,您可以将每个单元格的 BackColor 分别设置为其他默认颜色.设置后,它的优先级将超过默认值. Color.Emtpy 是一个 extra 值,表示:显示默认颜色.

Note that you can set each cell's BackColor individually to some other color that the default color. When it is set it takes precedence over the default. Color.Emtpy is an extra values that means: display the default color.

MSDN :

Color Empty:指定此Color结构是否未初始化.

Color Empty   :   Specifies whether this Color structure is uninitialized.

要查看显示的内容,您只需对其进行测试:

To find out what is displayed you can simply test for it:

Color c = someCell.Style.BackColor == Color.Emtpy ? 
          dgv.DefaultCellStyle.BackColor: someCell.Style.BackColor;

另一种测试方法是

Another way to test is the color.IsEmpty function.

您还可以通过将其设置为 Color.Emtpy 重置已设置为默认颜色的颜色.并且当默认颜色更改时,它也会更改.

And you can also reset a color you have set to the default color by setting it to Color.Emtpy. And when the default color changes it will change also.

示例:

您要用暗淡的背景色标记带有问题的单元格.在用户进入编辑模式之前,默认颜色为浅灰色.然后变成白色.

You want to mark cell with issues with a rosy back color. The default color is light gray until user enters edit mode; then is changes to white.

玫瑰色的细胞不会跟随,因为它们具有单独的颜色设置.

The rosy cells will not follow because they have an individual color set.

解决问题后,用户离开单元格,然后重置其颜色:但不是白色,而是 Color.Empty .

After correcting the issue the user leaves the cell and you reset its color: But not to white but to Color.Empty.

现在,当用户离开编辑模式时,所有没有问题的单元格都将恢复为默认的浏览颜色,浅灰色.

Now when the user leaves edit mode all cells without issues turn back to the default browse color light gray..

这篇关于C#datagridview.单元格背景色为空,尽管DefaultCellStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆