programmerist programmerist - 1 year ago 87
ASP.NET (C#) Question

How to handle currency format of decmal from sql inside of GridView Eval?

My local machine is Turkish, if i run my aplication , i can see easly my decimal value from mysql database like is my desire and everything is right what i want. i used "Text='<%#Eval("Veri","{0:N}")%>'". it is working great in my local but if i publish it result below. i want to see ny decimal like :

DB           My LOCAL GridVIEW  My Remote GridView

234567    234.567,00                  234,567.00

1234567      1.234.567,00            1,234,567.00

My GridView Code:

<asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Veri">
<asp:TextBox ID="txtVeri" runat="server" MaxLength="16" Enabled="false" Text='<%#Eval("Veri","{0:N}")%>' CssClass="form-control input-sm" Style="width: 100%;"></asp:TextBox>

This is my db decimal:
enter image description here
And my awesome result: ( I WANT THIS)
enter image description here

ON THE OTHER HAND : if i publish my application to Customer : ( I DONT WANT THIS)

enter image description here

Answer Source

With the snippet below you can set the specific number format settings for the page with the GridView on Page_PreInit without changing the whole UI language. This will of course affect all number formats on the page, not just the GridView.

protected void Page_PreInit(object sender, EventArgs e)
        CultureInfo newCultureInfo = (CultureInfo)CultureInfo.CurrentCulture.Clone();
        NumberFormatInfo numberFormatInfo = (NumberFormatInfo)newCultureInfo.NumberFormat.Clone();
        numberFormatInfo.NumberDecimalSeparator = ",";
        numberFormatInfo.NumberGroupSeparator = ".";
        numberFormatInfo.NumberDecimalDigits = 2;
        newCultureInfo.NumberFormat = numberFormatInfo;
        System.Threading.Thread.CurrentThread.CurrentCulture = newCultureInfo;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download