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

Edit column formatted as percentage cannot update

1 Answer 161 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 06 May 2011, 05:53 PM
I have a radGrid which displays a couple of GridBoundColumns representing decimal data fields in editmode as a Percentage = DataFormatString="{p:0}", but if I go to edit one of these and click update I generate the following error, since the other textbox is displaying with "  %"  in the value, ..

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format.

How can I have my cake and eat it too? I want to Display percentages as whole numbers and edit them the same way?
grid is named rdAsset

 

 

<

 

telerik:GridBoundColumn DataField="Target" DataType="System.Decimal" DataFormatString="{0:p2}"  HeaderText="Target%" SortExpression="Target" UniqueName="Target">

 

 

 

</telerik:GridBoundColumn>

 

 

 

 

 

 

 

 

<telerik:GridBoundColumn DataField="TargetRange" DataType="System.Decimal" DataFormatString="{0:p2}"

 

 

 

HeaderText="Target Range%" SortExpression="TargetRange" UniqueName="TargetRange">

 

 

</telerik:GridBoundColumn>

 

 

 

 

In the code behind, I have...

 

 

 

protected void rdAsset_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)

 

 

{

 

 

if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode )

 

 

{

 

 

GridEditFormItem item = e.Item as GridEditFormItem;

 

 

 

TextBox target = item["Target"].Controls[0] as TextBox;

 

 

target.Text =

String.Format("{0:p2}", (item.DataItem as DataRowView).Row["Target"]);

 

 

 

TextBox targetRange = item["TargetRange"].Controls[0] as TextBox;

 

 

targetRange.Text =

String.Format("{0:p2}", (item.DataItem as DataRowView).Row["TargetRange"]);

 

 

 

}

1 Answer, 1 is accepted

Sort by
0
Gimmik
Top achievements
Rank 1
answered on 11 May 2011, 09:07 PM
Hi Tim,

I don't think there is any way to do that out-of-the-box. It can be accomplished programmatically with a little effort though. There are two different steps required. First you must display the raw data in a formatted string, then you must convert a formatted string into raw data.

1) Format raw data:
You'll have to manually bind your data in the code-behind. Once your data is loaded into a structure, you can edit it however you want. I would create a new column of type String. Then convert your decimals to string types, adding a +"%" to the end. Display this column in the RadGrid instead of the decimal version. Since the column is a string at this point, it will display the same way in the edit box.

2) Convert formatted string to raw data:
This step is more tricky. You'll need to write a method that will convert the string into a number. That method will then insert the raw data into the database. You'll have to handle your own validation and exception handling. This method would replace the standard method that inserts data into the database.

So - It's definitively doable if you want to spend the time. One simple change that you could makes right now would be to change the column type to GirdNumericColumn. That would enforce a little data extra integrity (which is always a good thing!)

Hope this helps,
-Gimmik
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Gimmik
Top achievements
Rank 1
Share this question
or