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

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

    I know you can change the value of what was edited in the LeavingEdit event via the NewValue property, but how do you change the value that is about to be edited?

    I have a percentage column that I want to display a percentage (such as 2%) as “2.0 %” instead of “0.02” I can do this. But I also want it edited as “2.0” instead of “0.02”– how do I do this? The value actually needs to be stored in the database as 0.02.

    Imported from legacy forums. Posted by marvin (had 3849 views)

    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)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Tommy! Worked great.

    Is there an easy way to expose the properties of the GridNumericTextBox without manually coding each one and without exposing the whole GridNumericTextBox?

    Imported from legacy forums. Posted by marvin (had 514 views)

    User (Old forums)
    Member
    Post count: 23064

    First add a second constructor like this:<code>public GridPercentageTextBox(Xceed.Grid.ICellEditor editor)
    {
    this.editor = editor;
    }</code>Then you can just do this: <code>Xceed.Grid.Editors.GridNumericTextBox numEdit = new Xceed.Grid.Editors.GridNumericTextBox();
    GridPercentageTextBox pctEdit = new GridPercentageTextBox(numEdit);</code>
    Use <b>numEdit</b> to access the properties, and use <b>pctEdit</b> as the cell editor.

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

    User (Old forums)
    Member
    Post count: 23064

    yep, it works just great and as spected. But I have another problem, when this cell is selected and start typeing, it don’t go to the editmode !!!!!!

    Imported from legacy forums. Posted by Moniruzzaman (had 4380 views)

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