Custom DataGridViewColumn value disappears after loosing focus

progrAmmar

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.

Following is the code:

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

}

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


}

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

In the designer

private GridControl.NumericDataColumn colRoll;

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

progrAmmar

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Timer not resuming after game loosing focus

From Dev

NSTextField not correctly refreshing after loosing focus

From Dev

Timer not resuming after game loosing focus

From Dev

Stop custom datatables search box from loosing focus

From Dev

Show another element when a text is clicked or make :focus display after loosing focus

From Dev

Show another element when a text is clicked or make :focus display after loosing focus

From Dev

Value disappears after click event

From Dev

Value disappears after click event

From Dev

DataGridViewColumn EditingControl edited value not reflecting

From Dev

NSUserDefaults Loosing Value in Swift

From Dev

The value gets copied to the textarea but disappears after a moment

From Dev

JQuery mobile text field not loosing focus

From Dev

How to avoid loosing focus on click of anchor tag?

From Dev

How to set a custom DataGridViewColumn for an autogenerated column?

From Dev

Loosing audio after processing in AVComposition

From Dev

Button Loosing Reference after .appendChild

From Dev

Why my httpsessionstate is loosing value?

From Dev

iOS 7 Custom Back Button disappears after modal ViewController disappeared

From Dev

How to clean up after my custom Preference disappears

From Dev

Custom font disappears after some time in interface builder Xcode 6

From Dev

Presented view controller disappears after animation using custom UIViewController animations

From Dev

Javascript textarea value only appears after focus?

From Dev

call method when text is changed without textbox loosing focus

From Dev

WPF ListBox selected item, stays selected while loosing focus

From Dev

Qt Main window loosing focus and going to background unexpectedly

From Dev

AutoHotkey - application loosing focus when enabling full screen mode

From Dev

Reset MAAS after loosing Juju configuration?

From Dev

Reset MAAS after loosing Juju configuration?

From Dev

loosing fps after nodes leave screen

Related Related

  1. 1

    Timer not resuming after game loosing focus

  2. 2

    NSTextField not correctly refreshing after loosing focus

  3. 3

    Timer not resuming after game loosing focus

  4. 4

    Stop custom datatables search box from loosing focus

  5. 5

    Show another element when a text is clicked or make :focus display after loosing focus

  6. 6

    Show another element when a text is clicked or make :focus display after loosing focus

  7. 7

    Value disappears after click event

  8. 8

    Value disappears after click event

  9. 9

    DataGridViewColumn EditingControl edited value not reflecting

  10. 10

    NSUserDefaults Loosing Value in Swift

  11. 11

    The value gets copied to the textarea but disappears after a moment

  12. 12

    JQuery mobile text field not loosing focus

  13. 13

    How to avoid loosing focus on click of anchor tag?

  14. 14

    How to set a custom DataGridViewColumn for an autogenerated column?

  15. 15

    Loosing audio after processing in AVComposition

  16. 16

    Button Loosing Reference after .appendChild

  17. 17

    Why my httpsessionstate is loosing value?

  18. 18

    iOS 7 Custom Back Button disappears after modal ViewController disappeared

  19. 19

    How to clean up after my custom Preference disappears

  20. 20

    Custom font disappears after some time in interface builder Xcode 6

  21. 21

    Presented view controller disappears after animation using custom UIViewController animations

  22. 22

    Javascript textarea value only appears after focus?

  23. 23

    call method when text is changed without textbox loosing focus

  24. 24

    WPF ListBox selected item, stays selected while loosing focus

  25. 25

    Qt Main window loosing focus and going to background unexpectedly

  26. 26

    AutoHotkey - application loosing focus when enabling full screen mode

  27. 27

    Reset MAAS after loosing Juju configuration?

  28. 28

    Reset MAAS after loosing Juju configuration?

  29. 29

    loosing fps after nodes leave screen

HotTag

Archive