Html 如何将网格视图页脚中的数字和ItemTemplate中的数字格式化为此格式(“,”,“.000”)
如何将网格视图页脚中的数字和ItemTemplate中的数字格式化为此格式(“####,###.000”) html格式的页脚:Html 如何将网格视图页脚中的数字和ItemTemplate中的数字格式化为此格式(“,”,“.000”),html,css,asp.net,Html,Css,Asp.net,如何将网格视图页脚中的数字和ItemTemplate中的数字格式化为此格式(“####,###.000”) html格式的页脚: <FooterTemplate> <asp:Label ID="totallblCredAmount" runat="server" /> </FooterTemplate> ItemTemplate html: <asp:TemplateField HeaderText="cre
<FooterTemplate>
<asp:Label ID="totallblCredAmount" runat="server" />
</FooterTemplate>
ItemTemplate html:
<asp:TemplateField HeaderText="credit">
<ItemTemplate>
<asp:Label runat="server" Text='<%#(Eval("credit"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
您必须将字符串值解析为decimal
,然后可以使用decimal.ToString(“#####,##.000”)
。但是如果将double
或decimal
值存储在ViewState
中,则只需相应地强制转换它,而不是使用ToString
将其转换为字符串
所以也许:
if (e.Row.RowType == DataControlRowType.Footer)
{
Label totallblCAmount = (Label)e.Row.FindControl("totallblDebAmount");
decimal pricValue = (decimal)ViewState["TotalPric"];
totallblCAmount.Text = pricValue.ToString("###,###.000");
Label totallblCredAmount = (Label)e.Row.FindControl("totallblCredAmount");
decimal priceValue = (decimal)ViewState["TotalPrice"];
totallblCAmount.Text = priceValue.ToString("###,###.000");
}
如果存储字符串,则必须按照前面提到的方式对其进行解析:
string stringPriceValue = (string)ViewState["TotalPrice"];
decimal priceValue = decimal.Parse( stringPriceValue );
totallblCAmount.Text = priceValue.ToString("###,###.000");
我建议不要将十进制数值存储为字符串,这不仅是因为您必须始终解析它,而且主要是因为您对本地化问题持开放态度
根据您在评论中关于如何在ItemTemplate
中设置标签格式的问题。如果要在aspx上执行此操作,可以执行以下操作:
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "credit", "{0:###,###.000}") %>' ID="LblCredit"></asp:Label>
您必须将字符串值解析为decimal
,然后可以使用decimal.ToString(“####,###.000”)
。GridView的数据源是什么?它在decimal pricValue=(decimal)ViewState[“TotalPric”]中返回一个错误;错误是(指定的强制转换无效。)@rere:那么您在那里存储的是什么?可能是一个double
或(如上所述)一个字符串。只有你和调试器知道。现在它可以工作了,但是关于项目模板的格式,你知道怎么做吗???
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "credit", "{0:###,###.000}") %>' ID="LblCredit"></asp:Label>
protected void GrdidView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// note that i've assigned an ID to the label
Label lblCredit = (Label) e.Row.FindControl("LblCredit");
DataRow row = ((DataRowView) e.Row.DataItem).Row; // maybe you need to change the type of the dataitem
double credit = row.Field<double>("credit");
lblCredit.Text = credit.ToString("###,###.000");
}
}