Ich habe folgenden Ausdruck für die Währungsformatierung in einem ASP.Net Gridview. Das Dollar-Format wird nicht angezeigt, obwohl kein Fehler vorliegt. Was ist der fehlende Punkt hier?String Formatierung funktioniert nicht mit Eval
<%# 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 hinter
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; }
}
Bearbeitet. Sie haben vergessen, auf eine Ganzzahl zu setzen. – Matt
Wie funktioniert es ohne "Casting" im ersten Ausdruck? – Lijo
Ich nehme an, dass Int32 (int) IFormattable implementiert und eine ToString (string format) -Methode bereitstellt, die String.Format abhängig von der angegebenen Formatvorlage aufruft. – Matt