Home Forums WinForms controls Xceed Grid for WinForms Decimals in NumericEditor Reply To: Decimals in NumericEditor

User (Old forums)
Member
Post count: 23064

The Decimals property determines the maximum number of decimals the text can have for it to be validated, not a limit to the number of decimals that can be typed when editing.  What you need to do is to NOT set this property on the editor, and instead use the ValidatingText event on the TextBox of the editor, to ensure it does what you want.

e.g. :

//At form_load
      t_Column2.CellEditorManager.ActivatingControl += new CellEditorEventHandler(numericEditor_ActivatingControl);
      t_Column2.CellEditorManager.DeactivatingControl += new CellEditorEventHandler(numericEditor_DeactivatingControl);

//Events handler
    private void numericEditor_ActivatingControl( object sender, CellEditorEventArgs e )
    {
      Xceed.Editors.WinNumericTextBox winNumericTextBox = e.Control as Xceed.Editors.WinNumericTextBox;

      winNumericTextBox.TextBoxArea.ValidatingText += new Xceed.Editors.ValidatingTextEventHandler( TextBoxArea_ValidatingText );
    }

    private void numericEditor_DeactivatingControl( object sender, CellEditorEventArgs e )
    {
      Xceed.Editors.WinNumericTextBox winNumericTextBox = e.Control as Xceed.Editors.WinNumericTextBox;

      winNumericTextBox.TextBoxArea.ValidatingText -= new Xceed.Editors.ValidatingTextEventHandler( TextBoxArea_ValidatingText );
    }

    private void TextBoxArea_ValidatingText( object sender, Xceed.Editors.ValidatingTextEventArgs e )
    {
      Xceed.Editors.NumericTextBoxArea numericTextBoxArea = sender as Xceed.Editors.NumericTextBoxArea;

      int decimalSeparatorIndex = e.EditText.IndexOf( ‘.’ );

      if( decimalSeparatorIndex != -1 )
      {
        string afterDecimalSeparator = e.EditText.Remove( 0, decimalSeparatorIndex + 1 );

        if( afterDecimalSeparator.Length > 2 )
          e.Valid = false;
      }

Imported from legacy forums. Posted by André (had 1977 views)