其他分享
首页 > 其他分享> > 失去焦点后,自定义DataGridViewColumn值消失

失去焦点后,自定义DataGridViewColumn值消失

作者:互联网

我已经为WinForms中的项目创建了一个自定义DataGridViewColumn.控件呈现完美,但是一旦输入值并移到另一个单元格,当我在CellEndEdit中签入时,该值就会消失并显示为null.

以下是代码:

class NumericEditControl : NumericTextBox, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public NumericEditControl()
    {
        this.Value = 0;
    }

    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.ForeColor = dataGridViewCellStyle.ForeColor;
        this.BackColor = dataGridViewCellStyle.BackColor;
    }

    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

    public object EditingControlFormattedValue
    {
        get
        {
            return this.Value;
        }
        set
        {
            this.Value = Convert.ToDouble(value);
        }
    }

    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }

    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }

    public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
    {
        switch (keyData & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }

    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {

    }

    public bool RepositionEditingControlOnValueChange
    {
        get { return false; }
    }

}

单元格类别如下:

public class NumericCell : DataGridViewTextBoxCell
{
    public NumericCell()
        : base()
    {
        this.Style.Format = "0";
    }

    public override void InitializeEditingControl(int rowIndex, object
    initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue,
            dataGridViewCellStyle);
        NumericEditControl ctl =
            DataGridView.EditingControl as NumericEditControl;
        // Use the default row value when Value property is null.
        if (this.Value == null)
        {
            ctl.Value = 0;
        }
        else
        {
            ctl.Value = (double)this.Value;
        }
    }

    public override Type EditType
    {
        get
        {
            return typeof(NumericEditControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            return typeof(double);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            return "0";
        }
    }


}

最后是DataGridViewColumn

public class NumericDataColumn : DataGridViewColumn
{
    public NumericDataColumn()
        : base(new NumericCell())
    {
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(NumericCell)))
            {
                throw new InvalidCastException("Must be a Numeric");
            }
            base.CellTemplate = value;
        }
    }
}

在设计师

private GridControl.NumericDataColumn colRoll;

它呈现控件,但我不明白为什么值消失了.你能帮我一下吗

解决方法:

事实证明,我们还必须处理控件的“文本/值”更改事件,直到并且除非该单元格是脏的,否则值将不会保留.所以我必须将以下方法添加到我的NumericEditControl类中

class NumericEditControl : NumericTextBox, IDataGridViewEditingControl
{
    //Old code here
    protected override void OnTextChanged(EventArgs e)
    {
        if (dataGridView != null)
        {
            valueChanged = true;

            this.dataGridView.NotifyCurrentCellDirty(true);
            base.OnTextChanged(e);
        }
    }
}

添加此方法可以挽救我的生命,并且我的控件现在可以正常工作了:D感谢您的输入.

标签:datagridviewcolumn,custom-controls,c,winforms
来源: https://codeday.me/bug/20191119/2033616.html