Home All Groups Group Topic Archive Search About

Gridview decimal point is a comma?

Author
20 Jan 2006 4:03 PM
ibiza
Hi all,

I've binded some data from a DB in a gridview and one of the values is
a float. Even if the flot is represented as 4.2 in the DB, it is
rendered as 4,2 in the gridview, with a comma. When I edit the row, it
is still a comma in the editable textbox. And if I click on Update that
way, it gives an error, that the input value cannot be converted to a
float, when doing the UPDATE. If I change the comma for a point, it
works, which I want.

Here is my question : How can I ensure that the data displayed in the
gridview, editable or not, renders with a point and not a comma?

I've been googling a lot without finding an answer yet...

Thank you very much!

ibiza

Author
20 Jan 2006 4:19 PM
Phillip Williams
You can format a BoundField on the GridView using the DataFormatString and
set the HtmlEncode property to false
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx
Show quote
"ibiza" wrote:

> Hi all,
>
> I've binded some data from a DB in a gridview and one of the values is
> a float. Even if the flot is represented as 4.2 in the DB, it is
> rendered as 4,2 in the gridview, with a comma. When I edit the row, it
> is still a comma in the editable textbox. And if I click on Update that
> way, it gives an error, that the input value cannot be converted to a
> float, when doing the UPDATE. If I change the comma for a point, it
> works, which I want.
>
> Here is my question : How can I ensure that the data displayed in the
> gridview, editable or not, renders with a point and not a comma?
>
> I've been googling a lot without finding an answer yet...
>
> Thank you very much!
>
> ibiza
>
>
Author
20 Jan 2006 4:28 PM
ibiza
Thanks for the quick answer!

On MSDN, they use the DataFormatString like this :

<asp:boundfield datafield="discount"
            dataformatstring="{0:F4}%"
            itemstyle-horizontalalign="Right"
            headertext="Discount"/>

How can I use it with a TemplateField? There is no DataFormatString
property for it...is it?

And why should the HtmlEncode property be set to false? I don't
understand that one

Thanks again for your help!

ibiza
Author
20 Jan 2006 4:49 PM
Phillip Williams
To bind the data to controls within a templatefield you would use either
DataBinder.Eval or the 2-way databinding syntax of Bind. Both accept a
formatting string expression, e.g.

<asp:TemplateField HeaderText="discount">
<EditItemTemplate>
    <asp:TextBox ID="txtNumber" runat="server" Text='<%# Bind("discount",
"{0:F4}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("discount",
"{0:F4}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

The reason for using HTMLEncode=false on the BoundField was explained on the
article whose link I gave you as follows:

"When the HtmlEncode property is true, the value of the field is HTML
encoded to its string representation before the formatting string is applied.
For some objects, such as dates, you might want to control how the object is
displayed with a formatting string. In those cases, you must set the
HtmlEncode property to false."

Show quote
"ibiza" wrote:

> Thanks for the quick answer!
>
> On MSDN, they use the DataFormatString like this :
>
> <asp:boundfield datafield="discount"
>             dataformatstring="{0:F4}%"
>             itemstyle-horizontalalign="Right"
>             headertext="Discount"/>
>
> How can I use it with a TemplateField? There is no DataFormatString
> property for it...is it?
>
> And why should the HtmlEncode property be set to false? I don't
> understand that one
>
> Thanks again for your help!
>
> ibiza
>
>
Author
20 Jan 2006 4:57 PM
ibiza
Okay, you're really helping me!

as DataFormatString, I think a TemplateField does not have the
HtmlEncode property...is there a workaround?

One last question : what here will change the comma to a point? For
example, Eval("discount", "{0:F4}") will format the field, but not
change the point to a comma, am I correct?

thanks again :-)
Author
20 Jan 2006 5:12 PM
Phillip Williams
The standard format expressions are dependent on the Page.Culture setting or
the global culture setting on your server. You might examine a bit further
the cultureinfo object:
http://msdn2.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx

You can however manipulate the format to any thing you want as explained in
detail in this section of articles:
http://msdn2.microsoft.com/en-us/library/26etazsy.aspx

For example you can type Eval("discount", "{0:#0,00}")  to get at least one
number followed by a comma then 2 decimals.

As for the DataFormatString for TemplateField the HtmlEncode issue does not
apply.

Show quote
"ibiza" wrote:

> Okay, you're really helping me!
>
> as DataFormatString, I think a TemplateField does not have the
> HtmlEncode property...is there a workaround?
>
> One last question : what here will change the comma to a point? For
> example, Eval("discount", "{0:F4}") will format the field, but not
> change the point to a comma, am I correct?
>
> thanks again :-)
>
>
Author
20 Jan 2006 5:30 PM
ibiza
Thank you very much for the Culture information.

I finally solved the issue with adding a <globalization culture="en-US"
/> directive in <system.web> from web.config. That way, I have no
formatting to do and it will work through my entire webapp.

Thanks again!

Bruno

AddThis Social Bookmark Button