Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 禁用或灰显DataGridView_C#_.net_Winforms_Datagridview - Fatal编程技术网

C# 禁用或灰显DataGridView

C# 禁用或灰显DataGridView,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,是否有任何简单的方法来禁用/灰显DataGridView?比如在做 dgv.Enabled = false dgv的外观不会更改。我见过有人附加以下内容: dgv.forecolor = gray dgv.columnheader.forecolor = gray 然而,这似乎很笨拙。有更好的方法吗?设置ReadOnly=false是否会改变外观?我认为这可能会使datagraid的“可点击”部分变灰,例如列标题。但是您仍然可以看到其中的数据。我假设您希望datagridview向用户显示信

是否有任何简单的方法来禁用/灰显DataGridView?比如在做

dgv.Enabled = false
dgv的外观不会更改。我见过有人附加以下内容:

dgv.forecolor = gray
dgv.columnheader.forecolor = gray

然而,这似乎很笨拙。有更好的方法吗?

设置ReadOnly=false是否会改变外观?我认为这可能会使datagraid的“可点击”部分变灰,例如列标题。但是您仍然可以看到其中的数据。

我假设您希望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上获得“禁用”外观的方式

仅为标题设置灰色不会改变它。您还需要将EnableHeaderVisualStyles切换为false

dgv.ForeColor = Color.Gray;
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Gray;
dgv.EnableHeadersVisualStyles = false;

我知道这是一个解决了的问题,但我想防止其他人损失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;
    }
}
按钮的C版本。灵感来源于sveilleux2。 私有void DataGridView1_已启用更改(对象发送方,事件参数e){ 如果(!DataGridView1.Enabled){ DataGridView1.DefaultCellStyle.BackColor=SystemColor.Control; DataGridView1.DefaultCellStyle.ForeColor=SystemColors.GrayText; DataGridView1.ColumnHeadersDefaultCellStyle.BackColor=SystemColor.Control; DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor=SystemColors.GrayText; //禁用两列按钮 对于(int i=0;i
}

sveilleux2的示例,仅在C#(即标记)和advanced中(允许您将其放在任何名称和任何数量的DataGridView上)


尽管这个问题有点老了,但我还是要在这里添加它——我的做法与其他方法不同,方法是覆盖控件上的
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);
        }
    }
}
//
///禁用栅格时使用的颜色
/// 
[类别(“外观”)、默认值(typeof(Color)、“透明”)、说明(“控件禁用时使用的颜色(应为透明)”)]
公共颜色禁用颜色{get;set;}
/// 
///禁用栅格时使用的颜色
/// 
[类别(“外观”)、默认值(50)、说明(“禁用颜色的Alpha通道值(0-255)”)]
public int DisableColorAlpha{get;set;}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
基础漆(e);
if(this.Enabled==false&&DisableColor!=Color.Transparent)
{
//绘制一个透明的长方体--模拟禁用
使用(笔刷b=新的SolidBrush(Color.FromArgb(DisableColorAlpha,DisableColor)))
{
e、 图形.圆角矩形(b,e.ClipRectangle);
}
}
}

这一点很好-但是,没有,外观保持不变。这只是为了防止单元格编辑失败,我对控件的视觉方面更感兴趣,因为它在视觉提示中被禁用;禁用按钮等时的相同行为。您是否在谈论更改单元格、列和按钮的颜色?你能再具体一点吗?我想我不太明白。事实上,我想我知道。但不幸的是,我认为在C#中没有更简单的方法。您需要做的是调用一个javascript函数,该函数将为您显示灰色。请尊重系统颜色并使用
SystemColors.Control
而不是硬编码的灰色!我还必须将DefaultCellStyle.SelectionBackColor添加到设置为控制颜色的内容列表中,否则网格中的第一个单元格将显示为白色背景
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;
        }
    }
/// <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);
        }
    }
}