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

[Solved] Radgrid currency edit column

4 Answers 315 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jessie
Top achievements
Rank 1
Jessie asked on 08 Jun 2009, 08:08 PM
How to make the currency editable column GridBoundColumn or GridMaskedColumn in radgrid input currency round by 2 like 77.45, prevent the user inputting 77.353333?

Thanks!

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Jun 2009, 05:54 AM
Hello Jessie,

You can set the DataFormatString for the boundcolumn such that it displays only two digits after the decimal point.

aspx (To display the required format when the column is in Normal mode):
<telerik:GridBoundColumn HeaderText="Value" UniqueName="Value"  
    DataFormatString="{0:N2}" DataField="Value">  
</telerik:GridBoundColumn>  

c#(To display the required format for the TextBox when the column is in EditMode):
protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
        { 
            TextBox tb = (e.Item as GridEditableItem)["Value"].Controls[0] as TextBox; 
            string dataFormatString = (RadGrid2.MasterTableView.GetColumn("Value"as GridBoundColumn).DataFormatString; 
            tb.Text = String.Format(dataFormatString, double.Parse(tb.Text)); 
        } 
    } 

Thanks
Princy.
0
Jessie
Top achievements
Rank 1
answered on 09 Jun 2009, 02:15 PM
Hi Princy,

Thanks for the replay. What I mean is not only for display, but also for prevent the user inputting  more than 2 digits after the dedimal point.

Jessie
0
Accepted
Princy
Top achievements
Rank 2
answered on 10 Jun 2009, 09:32 AM
Hello Jessie,

To prevent the user from entering more than 2 digits after the dedimal point you would have to validate your textbox. Check out the code that I implemented for the same scenario. You can alter the code according to your requirement:
c#:
 protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode ) 
        { 
            TextBox txtbx = (e.Item as GridEditableItem)["Value"].Controls[0] as TextBox; 
            txtbx.Attributes.Add("onKeyDown""Validate(this)"); 
        } 
    } 

js:
function Validate(ctrl) 
   var value=ctrl.value;   
   if(value!=""
    { 
        var index=value.indexOf('.'
        if(index!=-1) 
        { 
            var v=value.substring(index); 
            if(v.length>2) 
            {               
              alert("You cannot enter more than two digits after the decimal point"); 
              ctrl.maxLength=value.length; 
            } 
        } 
    } 

Thanks
Princy.
0
Jessie
Top achievements
Rank 1
answered on 10 Jun 2009, 01:35 PM
Thanks for all your help, Princy!
Tags
Grid
Asked by
Jessie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jessie
Top achievements
Rank 1
Share this question
or