简体   繁体   English

不能在 DataGridView 单元格 BackColor 中使用 Color.FromArgb

[英]Cannot use Color.FromArgb in DataGridView cell BackColor

I am looping through a DataGridView control and adding rows dynamically.我正在遍历DataGridView控件并动态添加行。 I am setting the BackColor property of each cell based on the following logic:我根据以下逻辑设置每个单元格的BackColor属性:

if (bidVolume != null)
{
    this.Rows[this.RowCount - 1].Cells[1].Style.BackColor = Color.Green;
}
else
{
    this.Rows[this.RowCount - 1].Cells[1].Style.BackColor = Color.FromArgb(150, Color.Green);
}

This is causing problems, the color is not properly set visually and when re-sizing the DataGridView it looks like this:这会导致问题,颜色在视觉上设置不正确,并且在重新调整DataGridView大小时,它看起来像这样:

在此处输入图片说明

When I don't use Color.FromArgb and just use Color.Red for example, then everything works fine ..例如,当我不使用Color.FromArgb而只使用Color.Red ,一切正常。

Is it possible to set a cell BackColor using Color.FromArgb ?是否可以使用Color.FromArgb设置单元格BackColor

Thanks谢谢

You cannot use Color.FromArgb , because DataGridView won't accept transparent colors.您不能使用Color.FromArgb ,因为DataGridView不接受透明颜色。 This is probably caused by the fact that the cells and DataGridView are not transparent (by default).这可能是由于单元格和 DataGridView 不透明(默认情况下)造成的。 What you are looking for is probalby this ;您正在寻找的可能是这个 you may want to set BackColor to color between White and Green.您可能希望将BackColor设置为白色和绿色之间的颜色。

If I am mistaken and this is not what you want please explain your need for alpha channel in the cell.如果我弄错了,这不是您想要的,请解释您对单元格中 alpha 通道的需求。

You got the reason why.你知道原因了。 To overcome this, use the protected SetStyle method to override the behaviour.为了克服这个问题,请使用受保护的SetStyle方法来覆盖该行为。 Something like:就像是:

class MyDgv : DataGridView
{
    public MyDgv()
    {
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //this is the key

        //and now you can do what you want.
        this.Rows[this.RowCount - 1].Cells[1].Style.BackColor = Color.FromArgb(150, Color.Green);
    }
}

From documentation: 从文档:

The BackColor property does not support transparent colors unless the SupportsTransparentBackColor value of System.Windows.Forms.ControlStyles is set to true. BackColor 属性不支持透明颜色,除非 System.Windows.Forms.ControlStyles 的 SupportsTransparentBackColor 值设置为 true。

The BackColor property is an ambient property. BackColor 属性是一个环境属性。 An ambient property is a control property that, if not set, is retrieved from the parent control.环境属性是一个控件属性,如果未设置,则从父控件中检索。 For example, a Button will have the same BackColor as its parent Form by default.例如,默认情况下,Button 将具有与其父 Form 相同的 BackColor。 For more information about ambient properties, see the AmbientProperties class or the Control class overview.有关环境属性的更多信息,请参阅 AmbientProperties 类或 Control 类概述。

Old question, but I ran into the same issue.老问题,但我遇到了同样的问题。 The simplest solution is just to use the Color.FromArgb() override that excludes the alpha parameter from the constructor.最简单的解决方案是使用Color.FromArgb()覆盖,从构造函数中排除 alpha 参数。 As long as you don't specify an alpha transparency, the color works perfectly.只要您不指定 alpha 透明度,颜色就可以完美工作。

public static Color SeaFoamGreen = Color.FromArgb(20, 125, 115); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM