This is a migrated thread and some comments may be shown as answers.

GridNumericColumn restrict number of decimal digits that can be typed in.

3 Answers 210 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mqsash
Top achievements
Rank 1
mqsash asked on 20 Mar 2013, 07:43 PM
Hi

Is it possible to restrict the number of decimal digits that users can enter on GridNumericColumns (on edit forms and in-place edit) and on RadNumericTextBoxes?

I know that using the DecimalDigits and DataFormatString={0:C2} will display only two decimal digits in the control, with this users can type in as many decimal digits as they want and when they tab out the control displays two decimal digits.

But what I want here is for users to not be able to type in more than 2 decimal digits when editing the field. 


Not sure if/how this can be done.

Thanks

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Mar 2013, 04:55 AM
Hi,

Try the following code.
C#:
  protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            RadNumericTextBox txt = (RadNumericTextBox)item["Uniquename"].Controls[0];
            txt.NumberFormat.DecimalDigits = 2;
        }
}

Thanks,
Shinu
0
mqsash
Top achievements
Rank 1
answered on 22 Mar 2013, 10:53 PM
Thanks Shinu,

But unfortunately that did not work :(
I already had the DecimalDigits=2 defined in the markup for the column, but I also tried your option.
In both cases, tabbing out of the control truncates the value to two decimal places, but what I want is not even allow users to be able to type in a 3rd decimal digit.

Thanks
0
Shinu
Top achievements
Rank 2
answered on 25 Mar 2013, 05:45 AM
Hi,

Try modifying the code as shown below.
C#:
void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
  {
      if (e.Item is GridEditableItem && e.Item.IsInEditMode)
      {
          GridEditableItem item = (GridEditableItem)e.Item;
          RadNumericTextBox txt = (RadNumericTextBox)item["Uniquename"].Controls[0];
          txt.NumberFormat.DecimalDigits = 2;
          txt.ClientEvents.OnKeyPress = "OnKeyPress";
      }
  }
JS:
function OnKeyPress(sender, args) {
      if (sender.get_numberFormat().DecimalDigits == 2) {
          args.set_cancel(true);
      }
  }

Thanks,
Shinu
Tags
Grid
Asked by
mqsash
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
mqsash
Top achievements
Rank 1
Share this question
or