【问题标题】:Disabling or greying out a DataGridView禁用或灰显 DataGridView
【发布时间】:2012-02-01 16:20:49
【问题描述】:

是否有任何简单的方法可以禁用/灰显 DataGridView?比如做的时候

dgv.Enabled = false

dgv 的外观没有改变。我见过有人附加以下内容:

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

但是,这似乎很笨拙。有没有更好的办法?

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

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

    【讨论】:

    • 好点 - 但是,不,外观保持不变。这只是防止单元格编辑 afaik
    【解决方案2】:

    我假设您希望 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
      }
    

    希望这会有所帮助。 :]

    【讨论】:

    • 谢谢,当控件被禁用以获得视觉提示时,我对控件的视觉方面更感兴趣;禁用按钮等时的行为相同。
    • 您是在谈论更改单元格列和按钮的颜色吗?你能再具体一点吗?我觉得我不太明白。
    • 其实,我想我知道。但不幸的是,我认为在 C# 中没有更简单的方法。您需要做的是调用一个 javascript 函数,它将为您显示为灰色。
    【解决方案3】:

    简单回答您的问题:不,没有更好的方法。

    MSDN 对这个话题大多保持沉默,但论坛却很热闹。手动将背景颜色设置为灰色是大多数人在 DGV 上获得“禁用”外观的方式。

    【讨论】:

    • 请尊重系统颜色并使用SystemColors.Control 而不是硬编码的灰色!
    【解决方案4】:

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

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

    【讨论】:

    • 如果您仍想查看标题文本,但禁用“外观”,请使用dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlDark;
    【解决方案5】:
    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
    

    【讨论】:

    • 我还必须将 DefaultCellStyle.SelectionBackColor 添加到设置为控制颜色的事物列表中,否则网格中的第一个单元格将以白色背景显示。
    【解决方案6】:

    我知道这是一个已解决的问题,但想防止其他人损失 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;
        }
    }
    

    }

    【讨论】:

      【解决方案7】:

      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;
              }
          }
      

      【讨论】:

        【解决方案8】:

        即使问题有点老了,我也会在此处添加此问题 - 我通过覆盖控件上的 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);
                }
            }
        }
        

        【讨论】:

        • 这是一个优雅的解决方案,没有任何副作用。我将 SystemColors.ControlLightLight 用于 DisableColor,将 150 用于 DisableColorAlpha,以实现与其他禁用控件的良好匹配。这种组合使文本变暗,使用黑色时会丢失。这种方法实际上也适用于缺少禁用外观的任何其他控件,包括用户控件。
        • 这个答案与@PrestonMcCormick 的建议相结合似乎是一个成功的组合!
        猜你喜欢
        • 2018-05-20
        • 2014-01-26
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多