DataFormatString 格式如下表:
| 形式 | 语法 | 结果 | 注释 |
| 数字 | {0:N2} | 12.36 | |
| 数字 | {0:N0} | 13 | |
| 货币 | {0:c2} | $12.36 | |
| 货币 | {0:c4} | $12.3656 | |
| 货币 | "¥{0:N2}" | ¥12.36 | |
| 科学计数法 | {0:E3} | 1.23E+001 | |
| 百分数 | {0:P} | 12.25% | P and p present the same. |
| 日期 | {0:D} | 2006年11月25日 | |
| 日期 | {0:d} | 2006-11-25 | |
| 日期 | {0:f} | 2006年11月25日 10:30 | |
| 日期 | {0:F} | 2006年11月25日 10:30:00 | |
| 日期 | {0:s} | 2006-11-26 10:30:00 | |
| 时间 | {0:T} | 10:30:00 |
怎么使用?
1、在绑定数据时,用法如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" BorderColor="#666666">
<HeaderStyle Wrap="false" CssClass="BorderTblTh"/>
<Columns>
<asp:BoundField DataField="rowtime" HeaderText="时间" DataFormatString="{0:yyy-MM-dd HH:mm:ss}">
<ItemStyle CssClass="BorderTblCell" Width="120"/>
</asp:BoundField>
<asp:BoundField DataField="total" HeaderText="服务次数" DataFormatString="{0:###,###,###,###,##0}">
<ItemStyle CssClass="BorderTblCell" Width="100"/>
</asp:BoundField>
<asp:BoundField DataField="BeUsedRate" HeaderText="使用比例" DataFormatString="{0:P2}">
<ItemStyle CssClass="BorderTblCell"/>
</asp:BoundField>
</Columns>
</asp:GridView>2、格式化数据时
double d=0.3422d;
d.ToString("p2");

本文详细介绍了如何在ASP.NET环境中利用DataFormatString格式化数据,并将其应用到GridView控件中,包括数字、货币、日期、时间等不同类型数据的格式化方法,以及如何在绑定数据时实现自定义格式化。
2637

被折叠的 条评论
为什么被折叠?



