I'll add this here even though the question is a bit old - I did it differently than these others by overriding the Paint
method on the control to draw a transparent box. I used a class that inherited from the base DataGridView
and then provided some additional properties and an override for the OnPaint
method. You might be able to do this in the Paint
event as well, but for me I already had made our own version of the control.
This has the benefit of not changing any row/cell color/formatting you've already setup and just want to dim out the control when its disabled.
Simply set the DisableColor
(to Black for instance) to make it dim out (you can also alter the alpha channel with the DisableColorAlpha
property). Otherwise it acts as it always did.
/// <summary>
/// Color used when the grid is disabled
/// </summary>
[Category("Appearance"), DefaultValue(typeof(Color), "Transparent"), Description("Color to use when the control is disabled (should be transparent)")]
public Color DisableColor { get; set; }
/// <summary>
/// Color used when the grid is disabled
/// </summary>
[Category("Appearance"), DefaultValue(50), Description("Alpha channel value for disabled color (0-255)")]
public int DisableColorAlpha { get; set; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Enabled == false && DisableColor != Color.Transparent)
{
// paint a transparent box -- simulate disable
using (Brush b = new SolidBrush(Color.FromArgb(DisableColorAlpha, DisableColor)))
{
e.Graphics.FillRectangle(b, e.ClipRectangle);
}
}
}