【发布时间】:2012-02-01 16:20:49
【问题描述】:
是否有任何简单的方法可以禁用/灰显 DataGridView?比如做的时候
dgv.Enabled = false
dgv 的外观没有改变。我见过有人附加以下内容:
dgv.forecolor = gray
dgv.columnheader.forecolor = gray
但是,这似乎很笨拙。有没有更好的办法?
【问题讨论】:
标签: c# .net winforms datagridview
是否有任何简单的方法可以禁用/灰显 DataGridView?比如做的时候
dgv.Enabled = false
dgv 的外观没有改变。我见过有人附加以下内容:
dgv.forecolor = gray
dgv.columnheader.forecolor = gray
但是,这似乎很笨拙。有没有更好的办法?
【问题讨论】:
标签: c# .net winforms datagridview
设置 ReadOnly = false 是否会改变外观?我认为可能会使数据网格的“可点击”部分(例如列标题)变灰。但您仍然可以看到其中的数据。
【讨论】:
我假设您希望 datagridview 向用户显示信息并拒绝用户以任何方式进行修改。
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
}
希望这会有所帮助。 :]
【讨论】:
简单回答您的问题:不,没有更好的方法。
MSDN 对这个话题大多保持沉默,但论坛却很热闹。手动将背景颜色设置为灰色是大多数人在 DGV 上获得“禁用”外观的方式。
【讨论】:
SystemColors.Control 而不是硬编码的灰色!
只是为标题设置灰色不会改变它。您还需要将 EnableHeadersVisualStyles 切换为 false。
dgv.ForeColor = Color.Gray;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Gray;
dgv.EnableHeadersVisualStyles = false;
【讨论】:
dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlDark;。
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
【讨论】:
我知道这是一个已解决的问题,但想防止其他人损失 1 小时。
//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;
}
}
}
【讨论】:
sveilleux2 的示例,仅在 C#(即标记)和高级(允许您将其放在任何名称和任意数量的 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;
}
}
【讨论】:
即使问题有点老了,我也会在此处添加此问题 - 我通过覆盖控件上的 Paint 方法来绘制透明框,这样做与其他问题不同。我使用了一个从基类DataGridView 继承的类,然后为OnPaint 方法提供了一些附加属性和覆盖。您也可以在 Paint 事件中执行此操作,但对我来说,我已经制作了我们自己的控件版本。
这样做的好处是不会更改您已经设置的任何行/单元格颜色/格式,并且只想在控件被禁用时使其变暗。
只需设置DisableColor(例如设置为黑色)使其变暗(您也可以使用DisableColorAlpha 属性更改Alpha 通道)。否则它会像往常一样工作。
/// <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);
}
}
}
【讨论】: