vb.net 如何在 GridView Column ItemTemplate 中将数字数据格式化为数千?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15344456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I format numerical data as thousands in a GridView Column ItemTemplate?
提问by Sergio Rosas
I have a gridview with columns like so:
我有一个像这样的列的网格视图:
<asp:TemplateField HeaderStyle-Width="75px">
<HeaderTemplate>
<asp:Label ID="lblHM1" Text="Hm1" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblM1" Text='<%# Eval("m1","{0:#0}")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
The numbers in this column are often greater than 1000, so I'd like to format them as such. For example, if the data in this column reads 11359, I'd like it to format the number as 11,359.
此列中的数字通常大于 1000,因此我想将它们格式化。例如,如果此列中的数据为11359,我希望将数字格式化为11,359。
I have attempted the following:
我尝试了以下方法:
<asp:TemplateField HeaderStyle-Width="75px">
<HeaderTemplate>
<asp:Label ID="lblHM1" Text="Hm1" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblM1" Text='<%# Eval("m1","{0:N0}")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
But the above generates an exception: Input string was not in a correct format
但是上面产生了一个异常: Input string was not in a correct format
What am I doing wrong?
我究竟做错了什么?
回答by Sergio Rosas
You could do:
你可以这样做:
<asp:Label ID="lblM1" Text='<%# Eval("m1","{0:0,0}")%>' runat="server"></asp:Label>
That should format 11239 as "11.239". The Group Separator would be different depending on your culture.
那应该将 11239 格式化为“11.239”。根据您的文化,组分隔符会有所不同。
Take a look to the documentation:
看一下文档:
and
和
Standard Numeric Format Strings
EDIT: By the way, it could be a completely different reason. It could be that you're sending the data in one culture, but .Net it's trying to parse with a different one that's not compatible.
编辑:顺便说一下,这可能是一个完全不同的原因。可能是您在一种文化中发送数据,但 .Net 试图用不兼容的不同文化进行解析。
回答by Nisha
I tried your format and it worked for me, thanks :)
我试过你的格式,它对我有用,谢谢:)
just change as follow and try if it works for u.
只需按以下方式更改并尝试它是否适合您。
'>
'>
instead of Eval use DataBinder.EVal.
而不是 Eval 使用 DataBinder.EVal。
回答by Mayank
simply use this-
简单地使用这个 -
Text='<%# Eval("m1","{0:0,0}") %>'

