11

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

我可以使用DisabledCheckBoxCell禁用整个列

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

这是 DataGridView 的片段,

来源专栏 | 目的地列
真 | 启用
true | 启用
假 | 禁用

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

4

4 回答 4

23

维杰,

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;

        }

    }
于 2012-04-27T19:47:28.793 回答
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);
                }
            }
        }
    }
}
于 2015-04-15T19:43:41.480 回答
0

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

protected void xxx_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if ((e.Row != null) && e.Row.RowType == DataControlRowType.DataRow)
于 2012-04-26T11:50:02.580 回答
-1
         foreach (DataGridViewRow row in dataGridView1.Rows)
         {
             var cell = dataGridView1[cellindex, row.Index];
             if (cell.Value != null )
                 if((bool)cell.Value == true)
             {
               ....
             }
         }
于 2018-03-19T09:15:00.407 回答