Reputation: 22662
I have following expression for currency formatting inside an ASP.Net Gridview. It does not show dollar format though there is no error. What is the missing point here?
<%# String.Format("{0:C}", Convert.ToString(Eval("Amount")) ) %>
MARKUP
<asp:GridView ID="grdFinancialAmount" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Emp ID">
<ItemTemplate>
<%# Eval("EmpID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<%# String.Format("{0:C}", Convert.ToString(Eval("Amount")) ) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
Financial fin1 = new Financial { EmpID = 1, Amount = 5678 };
Financial fin2 = new Financial { EmpID = 2, Amount = -111111 };
List<Financial> accounts = new List<Financial>();
accounts.Add(fin1);
accounts.Add(fin2);
grdFinancialAmount.DataSource = accounts;
grdFinancialAmount.DataBind();
}
public class Financial
{
public int EmpID { get; set; }
public int Amount { get; set; }
}
Upvotes: 5
Views: 41035
Reputation: 5877
Eval
accepts a string format, and there is no need for these hacks.
As simple as: <%# Eval("Amount", "{0:C}") %>
Upvotes: 9
Reputation: 239
try this, works for me. (.NET 4.5 C#, in a gridview)
<%#Eval("Amout", "{0:C}").ToString()%>
Upvotes: 6
Reputation: 1678
Why not just do either...
<%# String.Format("{0:C}", Eval("Amount") ) %>
or
<%# ((int)Eval("Amount")).ToString("C") %>
Looks to me like you are trying to convert Amount to a string twice, and you can't format a string as currency.
Upvotes: 20
Reputation: 148624
try this :
<%# String.Format("{0:C}", int.Parse(DataBinder.Eval(Container.DataItem, "Amount").ToString())) %>
Upvotes: 3