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

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)