Disabling or greying out a DataGridView
Solution 1
Simple answer to your question: no, there isn't a better way.
MSDN is mostly silent on the topic but the forums are abuzz. Manually setting the background colour to Gray is how most people get "disabled" look on the DGV.
Solution 2
Private Sub DataGridView1_EnabledChanged(sender As Object, e As EventArgs) Handles DataGridView1.EnabledChanged
If Not DataGridView1.Enabled Then
DataGridView1.DefaultCellStyle.BackColor = SystemColors.Control
DataGridView1.DefaultCellStyle.ForeColor = SystemColors.GrayText
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText
DataGridView1.CurrentCell = Nothing
DataGridView1.ReadOnly = True
DataGridView1.EnableHeadersVisualStyles = False
Else
DataGridView1.DefaultCellStyle.BackColor = SystemColors.Window
DataGridView1.DefaultCellStyle.ForeColor = SystemColors.ControlText
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText
DataGridView1.ReadOnly = False
DataGridView1.EnableHeadersVisualStyles = True
End If
End Sub
Solution 3
sveilleux2's example, only in C# (which is the tag) and advanced (allows you to put it on any name and on any number of DataGridViews)
private void DataGridView_EnabledChanged(object sender, EventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (!dgv.Enabled) {
dgv.DefaultCellStyle.BackColor = SystemColors.Control;
dgv.DefaultCellStyle.ForeColor = SystemColors.GrayText;
dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText;
dgv.CurrentCell = null;
dgv.ReadOnly = true;
dgv.EnableHeadersVisualStyles = false;
}
else {
dgv.DefaultCellStyle.BackColor = SystemColors.Window;
dgv.DefaultCellStyle.ForeColor = SystemColors.ControlText;
dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText;
dgv.ReadOnly = false;
dgv.EnableHeadersVisualStyles = true;
}
}
Solution 4
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);
}
}
}
Solution 5
Just setting gray color for header will not change it. You also need to switch EnableHeadersVisualStyles to false.
dgv.ForeColor = Color.Gray;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Gray;
dgv.EnableHeadersVisualStyles = false;
Jeb
Updated on August 18, 2022Comments
-
Jeb 2 monthsIs there any easy way to disable/grey out a DataGridView? For instance when doing
dgv.Enabled = falseThe appearance of the dgv does not change. I have seen people appending the following:
dgv.forecolor = gray dgv.columnheader.forecolor = grayHowever, this seems clumsy. Is there a better way?