DataGrid-資料格式設定運算式

來源:互聯網
上載者:User

資料格式設定運算式

.NET Framework 格式設定運算式,它在資料顯示在列中之前先應用於資料。
此運算式由可選靜態文本和用以下格式表示的格式說明符組成:

{0:format specifier}

0 是參數索引,它指示列中要格式化的資料元素;因此,通常用零來指示第一個(且唯一的)元素。
format specifier 前面有一個冒號 (:),它由一個或多個字母組成,指示如何格式化資料。
可以使用的格式說明符取決於要格式化的資料類型:日期、數字或其他類型。

下表顯示了不同資料類型的格式設定運算式的樣本。有關格式設定運算式的更多資訊,請參見格式化類型。 

格式設定運算式 應用於此資料類型 說明
Price: {0:C} numeric/decimal 顯示“Price:”,後跟以貨幣格式表示的數字。貨幣格式取決於通過 Page 指令或 Web.config 檔案中的地區性屬性指定的地區性設定。
{0:D4} integer(不能和小數一起使用。) 在由零填充的四個字元寬的欄位中顯示整數。
{0:N2}% numeric 顯示精確到小數點後兩位的數字,後跟“%”。
{0:000.0} numeric/decimal 四捨五入到小數點後一位的數字。不到三位的數字用零填充。
{0:D} date/datetime 長日期格式(“Thursday, August 06, 1996”)。日期格式取決於頁或 Web.config 檔案的地區性設定。
{0:d} date/datetime 短日期格式(“12/31/99”)。
{0:yy-MM-dd} date/datetime 用數位年-月-日表示的日期(96-08-06)。

ASP.NET設定資料格式應用樣本:
       {0:d}     YY-MM-DD
       {0:p}     百分比00.00%
       {0:N2}  12.68
       {0:N0}  13
       {0:c2}    $12.68
       {0:d}      3/23/2003       
       {0:T}     12:00:00 AM
       {0:男;;女}

DataGrid資料格式的Format-- DataFormatString

DataFormatString="{0:格式字串}"

如原來的資料為「12.34」,若格式設定為 {0:N1},則輸出為「12.3」

格式字串 資料 結果
"{0:C}" 12345.6789  -> $12,345.68
"{0:C}" -12345.6789 -> ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 -> 00012345
"{0:E}" 12345.6789 -> 1234568E+004
"{0:E10}" 12345.6789 -> 1.2345678900E+004
"{0:F}" 12345.6789 -> 12345.68
"{0:F0}" 12345.6789 -> 12346
"{0:G}" 12345.6789 -> 12345.6789
"{0:G7}" 123456789 -> 1.234568E8
"{0:N}" 12345.6789 -> 12,345.68
"{0:N4}" 123456789 -> 123,456,789.0000
"Total: {0:C}" 12345.6789 -> Total: $12345.68

DateTime.Now.ToString("yyyy-MM-dd");//這種模式最好
aspx:
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="204px">
<Columns>
<asp:BoundColumn DataField="date" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>

</asp:datagrid>

 

    在我們從商務邏輯層獲得資料實體時候,接下來的事情就是要綁定到控制項中。資料實體中的一些欄位可以直接綁定到介面中,但是有一些欄位需要重新格式化格式。比如貨幣單位欄位,需要顯示貨幣符號和每隔三位顯示分隔字元;再比如日期欄位,資料庫中存放的是日期和時間,但是在介面上需要按照XXXX年XX月XX日的格式顯示。這時候我們就用到了DataFormatString屬性。

<asp:GridView ID="grvResult" runat="server" AutoGenerateColumns="False" Width="100%">

    <Columns>

        <asp:BoundField HeaderText="預定日期" DataField="OperationDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">

        </asp:BoundField>     

        <asp:BoundField HeaderText="訂單總計" DataField="TotalRate" DataFormatString="{0:C}" HtmlEncode="False">

        </asp:BoundField>

    </Columns>

</asp:GridView>

例如上面的代碼展示了日期和貨幣兩種綁定方式。DataFormatString中的{0}是固定的格式,這和String.Fromat(“{0}”, someString)中的{0}是一個用法,表示綁定內容相關的參數索引編號。然後,在後面加入格式化字串,具體的使用方法可以參考MSDN。

這裡需要注意以下幾點
1. 在GridView中的asp:BoundField使用DataFormatString必須設定屬性HtmlEncode="False",否則不起作用。
2. 如果需要使用日期類型的格式化字串,必須資料實體中對應的欄位也應該日起類型的。
3. 格式化字串C代表貨幣單位,需要繫結資料類型應該是數字類型的。如果是字串類型的不起作用,需要手動添加格式化字串為DataFormatString="¥{0:C}"。

總結:
     GridView中使用DataFromatString與在DataGrid中使用起來有些不同的!在GridView中的BoundField新增了HtmlEncode 屬性,且預設是true,這就使得DataFromatString失效!

string.format格式結果

String.Format

 (C) Currency: . . . . . . . . ($123.00)

(D) Decimal:. . . . . . . . . -123

(E) Scientific: . . . . . . . -1.234500E+002

(F) Fixed point:. . . . . . . -123.45

(G) General:. . . . . . . . . -123

(N) Number: . . . . . . . . . -123.00

(P) Percent:. . . . . . . . . -12,345.00 %

(R) Round-trip: . . . . . . . -123.45

(X) Hexadecimal:. . . . . . . FFFFFF85

(d) Short date: . . . . . . . 6/26/2004

(D) Long date:. . . . . . . . Saturday, June 26, 2004

(t) Short time: . . . . . . . 8:11 PM

(T) Long time:. . . . . . . . 8:11:04 PM

(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM

(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM

(g) General date/short time:. 6/26/2004 8:11 PM

(G) General date/long time: . 6/26/2004 8:11:04 PM

(M) Month:. . . . . . . . . . June 26

(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT

(s) Sortable: . . . . . . . . 2004-06-26T20:11:04

(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)

(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM

(Y) Year: . . . . . . . . . . June, 2004

(G) General:. . . . . . . . . Green

(F) Flags:. . . . . . . . . . Green (flags or integer)

(D) Decimal number: . . . . . 3

(X) Hexadecimal:. . . . . . . 00000003

 

 

說明:
String.Format
將指定的 String 中的每個格式項替換為相應對象的值的文本等效項。

例子:

int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了 {1} 次.", szName, iVisit));

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1789288

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.