5

How do you do separate numbers with commas for every thousand on asp.net gridview (with c# in the back)

I have been trying the following lines (individually) in the backend(nothing works, they just cut off the decimal places)

row["Applied_Amount_Varience_Sum"] = string.Format("{0:#,###0}", Convert.ToDecimal(row["Applied_Amount_Varience_Sum"]));
row["Applied_Amount_Varience_Sum"] = Convert.ToDecimal(row["Applied_Amount_Varience_Sum"]).ToString("#,##0.00"); 
row["Applied_Amount_Varience_Sum"] = String.Format("{0:n}", Convert.ToDecimal(row["Applied_Amount_Varience_Sum"]));
row["Applied_Amount_Varience_Sum"] = Convert.ToDecimal(row["Applied_Amount_Varience_Sum"]).ToString("N0");

row is a reference to the table in the database (I am looping through each object returned and going through the rows)

I have putting this where I bind to the gridview. So basically I right before it binds I change those attributes to have a comma but it doesn't do that

The lines I tried will change this

1092.00

to this

1092

but I am trying to achieve this

1,092

* EDIT *

This is within a template field (because I needed it to be a link with an onclick function

<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Applied Amount Variance">
  <ItemTemplate>
    <asp:LinkButton ID="LbPath" runat="server" 
                    Text='<%# Eval("Applied_Amount_Varience_Sum") %>'
                    CommandName="BindExpand" 
                    OnCommand="BindExpand"
                    CommandArgument='<%#Bind("Applied_Amount_Varience_Sum") %>'>
    </asp:LinkButton>
  </ItemTemplate>
</asp:TemplateField>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Kevin
  • 1,574
  • 4
  • 19
  • 45

2 Answers2

9

IF you are looking for Currency use

DataFormatString="{0:c}"

otherwise use

DataFormatString="{0:N2}"

Try

    Text='<%# Eval("Applied_Amount_Varience_Sum","{0:c}") %>'

or

    Text='<%# Eval("Applied_Amount_Varience_Sum","{0:N2}") %>'
wolfeh
  • 666
  • 1
  • 8
  • 23
4
private string commaSeparateNumber(string value)
{
    return String.Format("{0:#,##0}", int.Parse(value));
}

works for me.

in yor first line "{0:#,###0}", drop one #

Kiryl
  • 95
  • 4