C# 如何禁用 DataGridView CheckBox 列中的特定复选框单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10332715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:26:23  来源:igfitidea点击:

How to disable particular check box cell in a DataGridView CheckBox column

c#winformsdatagridview

提问by Vijay Balkawade

I have a winForm with a DataGridView control. It contains 5 columns, one of them is a CheckBox column. I want to enable/disable checkbox cell of this column based on the value present in another column at the same row.

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

I can disable entire column using DisabledCheckBoxCell

我可以使用DisabledCheckBoxCell禁用整列

But it makes entire column in disabled state.

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

Here is a snippet of DataGridView,

这是 DataGridView 的一个片段,

SourceColumn | DestinationColumn
true                  | enabled
true                  | enabled
false                 | disabled

来源专栏 | DestinationColumn
true | 启用
真| 启用
假 | 残疾

Does anyone have idea, how this can be achieved in .Net.

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

回答by Johnny_D

Try using RowDataBound event for your GridView. You can cast eventargs to row, find control on this row and disable it. This event fires for each row of gridview, even for headers and footers, so try not to catch exception. Here is some code might be useful to you

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

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

回答by mchicago

Vijay,

维杰,

DataGridViewCheckBoxColumn does not have property called disabled so by changing the style of the checkbox you can make it look like as if it is disabled. Look at the following code.

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;

        }

    }

回答by test

I ended up doing something like this to get an actual disabled checkbox to show up:

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

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

回答by Reza Feizi

         foreach (DataGridViewRow row in dataGridView1.Rows)
         {
             var cell = dataGridView1[cellindex, row.Index];
             if (cell.Value != null )
                 if((bool)cell.Value == true)
             {
               ....
             }
         }