Gridview decimal point is a comma?

Discussion in 'ASP .Net' started by ibiza, Jan 20, 2006.

  1. ibiza

    ibiza Guest

    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
    ibiza, Jan 20, 2006
    #1
    1. Advertising

  2. 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
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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
    >
    >
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 20, 2006
    #2
    1. Advertising

  3. ibiza

    ibiza Guest

    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
    ibiza, Jan 20, 2006
    #3
  4. 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."

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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
    >
    >
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 20, 2006
    #4
  5. ibiza

    ibiza Guest

    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 :)
    ibiza, Jan 20, 2006
    #5
  6. 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.

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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 :)
    >
    >
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 20, 2006
    #6
  7. ibiza

    ibiza Guest

    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
    ibiza, Jan 20, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasool
    Replies:
    3
    Views:
    519
    mikeb
    Mar 3, 2004
  2. Guru
    Replies:
    1
    Views:
    6,899
    Michael Borgwardt
    Jul 30, 2004
  3. JohnC
    Replies:
    7
    Views:
    1,688
    Peter Flynn
    Jun 7, 2008
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,298
    Saraswati lakki
    Jan 6, 2012
  5. PERO
    Replies:
    4
    Views:
    448
    Brian McCauley
    Apr 13, 2007
Loading...

Share This Page