Gridview decimal point is a comma?

I

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
 
I

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
 
G

Guest

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."
 
I

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 :)
 
G

Guest

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.
 
I

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top