失去重心后的自定义的DataGridViewColumn值消失 [英] Custom DataGridViewColumn value disappears after loosing focus

查看:171
本文介绍了失去重心后的自定义的DataGridViewColumn值消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义的DataGridViewColumn为我的WinForms项目。该控件呈现完美的,但是当我输入数值并移动到另一个单元格,该值消失,并出现为空,当我在CellEndEdit检查。

I have created a custom DataGridViewColumn for my project in WinForms. The control renders perfectly, but once I input the value and move to another cell, the value disappears and comes up as null when I check in CellEndEdit.

以下是代码

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

}



Cell类如下:

The cell class is as follows:

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

And finally the 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;



它呈现的控制,但我不明白为什么值正在消失。能否请你帮我出

It renders the control but I cannot understand why the value is disappearing. Can you please help me out

推荐答案

事实证明,我们必须处理控件的文本/值更改事件,以及直到除非小区脏值将不保留。于是,我只好下面的方法添加到我的NumericEditControl类

It turns out that we have to handle the control's Text / Value change event as well and until and unless the cell is dirty the value will not retain. So I had to add the following method to my NumericEditControl class

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感谢输入

Adding this method saved my life and my control is working properly now :D thanks for the input.

这篇关于失去重心后的自定义的DataGridViewColumn值消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆