Reputation: 3799
Is there any easy way to disable/grey out a DataGridView? For instance when doing
dgv.Enabled = false
The appearance of the dgv does not change. I have seen people appending the following:
dgv.forecolor = gray
dgv.columnheader.forecolor = gray
However, this seems clumsy. Is there a better way?
Upvotes: 25
Views: 35140
Reputation: 160
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);
}
}
}
Upvotes: 5
Reputation: 572
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;
}
}
Upvotes: 8
Reputation: 573
I understand this is a solved one but want to prevent the loss of 1 hour for some one else.
//C# version for buttons also. Inspired by sveilleux2.
private void DataGridView1_EnabledChanged(object sender, EventArgs e){
if (!DataGridView1.Enabled){
DataGridView1.DefaultCellStyle.BackColor = SystemColors.Control;
DataGridView1.DefaultCellStyle.ForeColor = SystemColors.GrayText;
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control;
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText;
//Disable two colums of buttons
for (int i = 0; i < DataGridView1.RowCount; i++){
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[1];
buttonCell.FlatStyle = FlatStyle.Popup;
buttonCell.Style.ForeColor = SystemColors.GrayText;
buttonCell.Style.BackColor = SystemColors.Control;
DataGridViewButtonCell buttonCell_2 = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[6];
buttonCell_2.FlatStyle = FlatStyle.Popup;
buttonCell_2.Style.ForeColor = SystemColors.GrayText;
buttonCell_2.Style.BackColor = SystemColors.Control;
}
DataGridView1.Columns[1].DefaultCellStyle.ForeColor = SystemColors.GrayText;
DataGridView1.Columns[1].DefaultCellStyle.BackColor = SystemColors.Control;
DataGridView1.ReadOnly = true;
DataGridView1.EnableHeadersVisualStyles = false;
DataGridView1.CurrentCell = null;
}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 = false;
//Enable two colums of buttons
for (int i = 0; i < DataGridView1.RowCount; i++){
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[1];
buttonCell.FlatStyle = FlatStyle.Standard;
DataGridViewButtonCell buttonCell_2 = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[6];
buttonCell_2.FlatStyle = FlatStyle.Standard;
}
}
}
Upvotes: 1
Reputation: 1512
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
Upvotes: 23
Reputation: 727
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;
Upvotes: 3
Reputation: 6886
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.
Upvotes: 15
Reputation: 648
I'm assuming you want the datagridview to display info to the user and deny the user the ability to modify in any way.
private void IntializeDataGridView()
{
dataGridViewTest.ReadOnly = true;
// you can code permissions or colors as well
dataGridViewTest.AllowUserToAddRows = false;
dataGridViewTest.AllowUserToDeleteRows = false;
dataGridViewTest.AllowUserToOrderColumns = false;
dataGridViewTest.BackgroundColor = Color.LightGray;
//so on and so forth
}
Hope this helps. :]
Upvotes: 0
Reputation: 2365
does setting the ReadOnly = false alter the appearance at all? I thought possibly that greyed out the 'clickable' parts of the datagraid such as column headers..but you can still see the data in it.
Upvotes: -1