Search code examples
asp.netsql-serveraspxgridviewcurrentculture

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


My local machine is Turkish, if i run my asp.net aplication , i can see easly my decimal value from mysql database like below.it 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">
                                        <ItemTemplate>
                                            <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>
                                        </ItemTemplate>
                                    </asp:TemplateField>

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 asp.net application to Customer : ( I DONT WANT THIS)

enter image description here


Solution

  • 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;
        }