字符串格式不工作评估和演示 [英] String Formatting Not working with Eval
本文介绍了字符串格式不工作评估和演示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下EX pression货币里面一个ASP.Net的GridView格式化。它不显示元格式,虽然没有发生错误。什么是缺少点吗?
<%#的String.Format({0:C},Convert.ToString(EVAL(额)))%>
MARKUP
< ASP:GridView控件ID =grdFinancialAmount=服务器的AutoGenerateColumns =假>
<列>
< ASP:模板列HEADERTEXT =的Emp ID>
<的ItemTemplate>
<%#的eval(的EmpID)%>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:模板列HEADERTEXT =量与GT;
<的ItemTemplate>
<%#的String.Format({0:C},Convert.ToString(EVAL(额)))%>
< / ItemTemplate中>
< / ASP:的TemplateField>
< /列>
< / ASP:GridView控件>
code背后
保护无效的Page_Load(对象发件人,EventArgs的)
{
金融FIN1 =新的金融{的EmpID = 1,金额= 5678};
金融FIN2 =新的金融{的EmpID = 2,金额= -111111};
名单<金融与GT;帐户=新的名单,其中,金融与GT;();
accounts.Add(FIN1);
accounts.Add(FIN2);
grdFinancialAmount.DataSource =账户;
grdFinancialAmount.DataBind();
}
公共类金融
{
公众诠释的EmpID {获得;组; }
公众诠释金额{获得;组; }
}
解决方案
为什么不只是做要么...
<%#的String.Format({0:C}中,eval(数量))%>
或
<%#((INT)的eval(数量))的ToString(C)%>
在我看来像你正试图金额转换为字符串两次,你不能格式化字符串作为货币。
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; }
}
解决方案
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.
这篇关于字符串格式不工作评估和演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文