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

时间:2012-04-26 11:44:08

标签: c# winforms datagridview

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

我可以使用DisabledCheckBoxCell

停用整列

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

以下是DataGridView的片段,

SourceColumn | DestinationColumn
是的|启用
是的|启用
假|禁用

有没有人知道如何在.Net中实现这一目标。

4 个答案:

答案 0 :(得分:21)

维杰,

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;

        }

    }

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

尝试为GridView使用RowDataBound事件。您可以将eventargs转换为row,在此行上查找控件并禁用它。 此事件会触发gridview的每一行,即使对于页眉和页脚也是如此,因此请尽量不要捕获异常。 以下是一些可能对您有用的代码

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

答案 3 :(得分:0)

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