溫馨提示×

c#數據格式化之DataFormatString怎么用

c#
小億
120
2023-10-23 17:54:30
欄目: 編程語言

在C#中,可以使用DataFormatString屬性來格式化數據。該屬性可以應用于數據綁定控件(如GridView、DataList等)中的字段或綁定表達式。

以下是使用DataFormatString屬性的示例:

  1. 在GridView中使用DataFormatString屬性:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="DateOfBirth" HeaderText="Date of Birth" DataFormatString="{0:dd/MM/yyyy}" />
        <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:C2}" />
    </Columns>
</asp:GridView>

在上面的示例中,DateOfBirth字段將按照“dd/MM/yyyy”格式進行顯示,Price字段將按照“C2”格式進行顯示(以貨幣格式顯示,保留兩位小數)。

  1. 在綁定表達式中使用DataFormatString屬性:
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DateOfBirth", "{0:dd/MM/yyyy}") %>'></asp:Label>

在上面的示例中,Label控件將顯示DateOfBirth字段的值,并按照“dd/MM/yyyy”格式進行顯示。

需要注意的是,DataFormatString屬性中的格式字符串語法遵循標準的.NET格式字符串規范。你可以根據需要選擇不同的格式字符串,如日期時間格式、貨幣格式、數字格式等??梢愿鶕唧w需求自定義格式字符串。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女