【问题标题】:How to disable particular check box cell in a DataGridView CheckBox column如何禁用 DataGridView CheckBox 列中的特定复选框单元格
【发布时间】:2012-04-26 11:44:08
【问题描述】:

我有一个带有 DataGridView 控件的 winForm。它包含 5 列,其中之一是 CheckBox 列。我想根据同一行另一列中的值启用/禁用该列的复选框单元格。

我可以使用 DisabledCheckBoxCell 禁用整个列

但它使整个列处于禁用状态。

这里是DataGridView的sn-p,

SourceColumn |目的地列
真                  |启用
真                  |启用
错误                 |禁用

有谁知道如何在 .Net 中实现这一点。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    维杰,

    DataGridViewCheckBoxColumn 没有名为 disabled 的属性,因此通过更改复选框的样式,您可以使其看起来好像已禁用。看下面的代码。

     private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (e.RowIndex == 1)
            {
                DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0];
                DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
                chkCell.Value = false;
                chkCell.FlatStyle = FlatStyle.Flat;
                chkCell.Style.ForeColor = Color.DarkGray;
                cell.ReadOnly = true;
    
            }
    
        }
    

    【讨论】:

    • 感谢您的回复。这正在处理一些小问题。我在列标题中也有复选框单元格。这也在影响他们。当我切换到其他 App 并返回原来的 App 时,UI 看起来很扭曲。
    【解决方案2】:

    我最终做了这样的事情来显示一个实际禁用的复选框:

    using System.Windows.Forms.VisualStyles;
    
    public partial class YourForm : Form
    {
    
        private static readonly VisualStyleRenderer DisabledCheckBoxRenderer;
    
        static YourForm()
        {
            DisabledCheckBoxRenderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.UncheckedDisabled);
        }
    
        private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                int checkBoxColumnIndex = this.yourCheckBoxColumn.Index;
                var checkCell = (DataGridViewCheckBoxCell)this.dataGridView[checkBoxColumnIndex, e.RowIndex];
                var bounds = this.dataGridView.GetCellDisplayRectangle(checkBoxColumnIndex , e.RowIndex, false);
    
                // i was drawing a disabled checkbox if i had set the cell to read only
                if (checkCell.ReadOnly)
                {
                    const int CheckBoxWidth = 16;
                    const int CheckBoxHeight = 16;
    
                    // not taking into consideration any cell style paddings
                    bounds.X += (bounds.Width - CheckBoxWidth) / 2;
                    bounds.Y += (bounds.Height - CheckBoxHeight) / 2;
                    bounds.Width = CheckBoxWidth;
                    bounds.Height = CheckBoxHeight;
    
                    if (VisualStyleRenderer.IsSupported)
                    {
    
                        // the typical way the checkbox will be drawn
                        DisabledCheckBoxRenderer.DrawBackground(e.Graphics, bounds);
                    }
                    else
                    {
    
                        // this method is only drawn if the visual styles of the application
                        // are turned off (this is for full support)
                        ControlPaint.DrawCheckBox(e.Graphics, bounds, ButtonState.Inactive);
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试为您的 GridView 使用 RowDataBound 事件。您可以将 eventargs 转换为行,在该行上找到控件并将其禁用。 此事件会为每一行 gridview 触发,即使是页眉和页脚,所以尽量不要捕获异常。 这里有一些代码可能对你有用

      protected void xxx_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if ((e.Row != null) && e.Row.RowType == DataControlRowType.DataRow)
      

      【讨论】:

      • 我想禁用相应行中的单元格,但在单元格上找不到启用的属性。我正在使用 RowPostPaint 事件。
      • 恐怕没有任何功能可以禁用单个单元格,但是您可以在单击单元格时执行某些操作,例如恢复要禁用的单元格的值
      • 是的,我在 CellEndEdit 事件中也尝试过,但没有得到尊重。复选框仍显示当前状态(选中/未选中)。
      • @Johnny_D 是的,它是一个 WinForm 应用程序。
      【解决方案4】:
               foreach (DataGridViewRow row in dataGridView1.Rows)
               {
                   var cell = dataGridView1[cellindex, row.Index];
                   if (cell.Value != null )
                       if((bool)cell.Value == true)
                   {
                     ....
                   }
               }
      

      【讨论】:

      • 您好,欢迎来到 Stack Overflow,仅代码的答案往往不受欢迎,所以请添加一些解释,说明您的代码的作用以及为什么它是解决此问题的合适解决方案(即使代码运行对您来说似乎很明显)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      相关资源
      最近更新 更多