将DataGridViewButtonCell转换为DataGridViewDisableButtonCell时出错

时间:2019-06-18 09:41:08

标签: c# winforms

我有错误 无法将类型为“ System.Windows.Forms.DataGridViewButtonCell”的对象转换为类型为“ Sento.DataGridViewDisableButtonCell”的对象。

我的目标是选中复选框后,该按钮将启用

我的代码:

private void GrdSento_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if(GrdSento.Columns[e.ColumnIndex].Name == "Check")
        {
            DataGridViewCheckBoxCell checkBoxCell = (DataGridViewCheckBoxCell)GrdSento.Rows[e.RowIndex].Cells["Check"];
            DataGridViewDisableButtonCell ButtonCell = (DataGridViewDisableButtonCell)GrdSento.Rows[e.RowIndex].Cells["Edit"];
            ButtonCell.Enabled = !(Boolean)checkBoxCell.Value;

            GrdSento.Invalidate();
        }
    }

我从以下链接复制:DataGridViewDisableButtonCell

public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
    private bool enabledValue;
    public bool Enabled
    {
        get
        {
            return enabledValue;
        }
        set
        {
            enabledValue = value;
        }
    }
    // By default, enable the button cell.
    public DataGridViewDisableButtonCell()
    {
        this.enabledValue = true;
    }
}

DataGridViewDisableButtonCell已经从DataGridViewButtonCell继承,但是仍然会出错。请帮助我。

1 个答案:

答案 0 :(得分:0)

在[Form] .Designer.cs文件中,更改

this.Edit = new System.Windows.Forms.DataGridViewButtonColumn();

this.Edit = new **DataGridViewDisableButtonColumn();**