Home Forums WinForms controls Xceed Grid for WinForms Creating a nullable numeric field

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #14273 |

    I have a numeric field which is nullable. I’m having a problem where the user is unable to set the value back to null after a numeric value is entered in the cell. When the user tries to clear out the value in the cell, the original value in the cell is automatically restored. This happen as soon as focus leaves the cell.

    I am unable to find an event which will allow me to handle this. LeavingEdit does NOT fire if the user clears the cell (it does if the user enters a regular value). I was able to handle the delete key for a cell so that the user can clear the cell by pressing “delete” if the editor is not active. However, I would also like the user to be able to simply clear the text. Any ideas? Thanks.

    Imported from legacy forums. Posted by bellx (had 1885 views)

    User (Old forums)
    Member
    Post count: 23064

    Here is my solution

    class NullableTextEditor : Xceed.Grid.Editors.TextEditor
    {
    private object _oldValue;

    public object oldValue {
    get { return _oldValue; }
    }

    public NullableTextEditor()
    {
    this.GettingControlValue += new Xceed.Grid.Editors.GettingEditorControlValueEventHandler(NullableTextEditor_GettingControlValue);
    }

    void NullableTextEditor_GettingControlValue(object sender, Xceed.Grid.Editors.GettingEditorControlValueEventArgs e)
    {
    if (e.CellValue == null) // we had a validation error
    {
    e.CellValue = ((NullableTextEditor)sender).oldValue;
    }

    // if still null.. at least reset to DBNull
    if (e.CellValue == null)
    e.CellValue = DBNull.Value;
    }

    protected override object GetControlValueCore(Control control, Cell cell, Type returnDataType)
    {

    string text = control.Text.ToString();
    object ret = null;

    if (text.Length > 0)
    {
    try
    {
    ret = base.GetControlValueCore(control, cell, returnDataType);
    }
    catch (Exception)
    {

    return null; // straight up null, not DBnull if validation error
    }

    return ret;
    }
    else
    return DBNull.Value;
    }

    protected override void SetControlValueCore(Control control, Cell cell)
    {
    base.SetControlValueCore(control, cell);
    _oldValue = cell.Value;
    }
    };

    Imported from legacy forums. Posted by bellx (had 3145 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.