在GridView中如何格式化Money型字段(downmoon)?
这段代码中,
amount为Money型字段,无论如何只能显示成
1234.5600
而不能显示成
1,234.56
用
也不行!
后来在MSDN上找到了答案
关键在于 HtmlEncode="False"
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="PKID"
DataMember="DefaultView" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="PKID" HeaderText="PKID" InsertVisible="False" ReadOnly="True"
SortExpression="PKID" />
<asp:TemplateField>
<HeaderTemplate>
amount</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtMoney" Text='<%# Decimal.Parse(DataBinder.Eval(Container.DataItem,"amount").ToString())%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TestID" HeaderText="TestID" SortExpression="TestID" />
<asp:BoundField DataField="testString" HeaderText="testString" SortExpression="testString" />
</Columns>
</asp:GridView>
DataMember="DefaultView" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="PKID" HeaderText="PKID" InsertVisible="False" ReadOnly="True"
SortExpression="PKID" />
<asp:TemplateField>
<HeaderTemplate>
amount</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtMoney" Text='<%# Decimal.Parse(DataBinder.Eval(Container.DataItem,"amount").ToString())%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TestID" HeaderText="TestID" SortExpression="TestID" />
<asp:BoundField DataField="testString" HeaderText="testString" SortExpression="testString" />
</Columns>
</asp:GridView>
这段代码中,
amount为Money型字段,无论如何只能显示成
1234.5600
而不能显示成
1,234.56
用
<asp:BoundField DataField="amount" HeaderText="amount" DataFormatString="{0:n2}" />
<asp:BoundField DataField="amount" HeaderText="amount" DataFormatString="{0:c2}" />
<asp:BoundField DataField="amount" HeaderText="amount" DataFormatString="{0:c2}" />
也不行!
后来在MSDN上找到了答案
<asp:BoundField DataField="amount" HeaderText="amount" DataFormatString="{0:#,###.00}" HtmlEncode="False" />
关键在于 HtmlEncode="False"