在 gridview 中格式化十进制值 [英] format decimal value in gridview
问题描述
我的 Gridview 中有一个绑定字段,它从数据库表中获取其值.
I have a bound field in my Gridview which is getting its value from a database table.
我已经获得了数据,但不知道如何在 gridview 中对其进行格式化.
I have got the data but don't know how to format it inside the gridview.
例如,我从下面获取总数据,如123456",但我想显示为123,456"
For example I get total data from below like "123456", but I want to display as "123,456"
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" />
我该怎么做?我需要将绑定字段转换为模板字段吗?但那之后我该怎么办.
How can I do this? Do I need to convert the bound field into a template field ? But what do i do after that.
请帮忙.
我使用了 DataFormatString="{0:n0}" 并解决了上述问题.
I have used DataFormatString="{0:n0}" and it solved the above problem.
我该怎么做:
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
超链接有需要格式化的数字...
the hyperlink has the number which need to be formatted...
推荐答案
Use DataFormat property :
Use DataFormat property :
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" DataFormatString="{0:n3}" />
对于您问题的第二部分,请使用 Eval 方法的第二个参数来格式化您的数据:
EDIT : For the second part of your question use Eval method's second parameter to format your data :
<%# Eval("NumFailedFiles", "{0:n3}") %>
那么你的模板将是这样的:
Then your template will be like that :
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server"
ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>'
Text='<%# Eval("NumFailedFiles", "{0:n3}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
这篇关于在 gridview 中格式化十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!