Home Forums WinForms controls Xceed Grid for WinForms how change value about to edit in GridNumericTextbox? Reply To: how change value about to edit in GridNumericTextbox?

User (Old forums)
Member
Post count: 23064

The only way I can think of to do this, is by creating a custom ICellEditor class. If you want to mimic the GridNumericTextBox, and just multiply or divide the value by 100 when getting or setting the value, you could try this (untested code):<code>public class GridPercentageTextBox : Xceed.Grid.ICellEditor
{
private Xceed.Grid.ICellEditor editor;
public GridPercentageTextBox()
{
editor = new Xceed.Grid.Editors.GridNumericTextBox();
}
#region ICellEditor Members
public bool IsInputChar(char charCode) { return editor.IsInputChar(charCode); }
public void ActivateControl(Cell cell) { editor.ActivateControl(cell); }
public Control Control { get { return editor.Control; } }
public bool IsActivationKey(System.Windows.Forms.Keys keyData) { return editor.IsActivationKey(keyData); }
public bool InPlace { get { return editor.InPlace; } }
public int GetFittedHeight(Xceed.Grid.AutoHeightMode mode) { return editor.GetFittedHeight(mode); }
public bool HandleActivationClick { get { return editor.HandleActivationClick; } }
public bool IsActivationChar(char charCode) { return editor.IsActivationChar(charCode); }
public bool IsInputKey(System.Windows.Forms.Keys keyData) { return editor.IsInputKey(keyData); }
public int GetFittedWidth() { return editor.GetFittedWidth(); }
<b>public object GetControlValue()
{
object value = editor.GetControlValue();
if (value == null || value is DBNull) return value;
else return ConvertToDouble(value) / 100.0;
}
public void SetControlValue(object value, IFormatProvider customFormatProvider)
{
if (value != null && !(value is DBNull)) value = ConvertToDouble(value) * 100.0;
editor.SetControlValue(value, customFormatProvider);
}</b>
#endregion

private double ConvertToDouble(object value)
{
if (value is double) return (double)value;
else if (value is string)
{
double result;
if (Double.TryParse(value as string, System.Globalization.NumberStyles.Any, null, out result)) return result;
else return 0.0;
}
else if (value is float || value is int || value is long) return Convert.ToDouble(value);
else return 0.0;
}
}</code>And in your code, replace your <b>GridNumericTextBox</b> with <b>GridPercentageTextBox</b>.

Imported from legacy forums. Posted by Tommy (had 242 views)