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>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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}") %>'
Method 2
private string commaSeparateNumber(string value)
{
return String.Format("{0:#,##0}", int.Parse(value));
}
works for me.
in yor first line “{0:#,###0}”, drop one #
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0