DataFormatString in GridView

I’ve been working on an ASP.NET project lately, which means it’s high time I write something down to build up my own knowledge base and track down my trail of learning how to code effectively. Hence this article, just another ASP.NET record.

Data Formatting Expression

Back on track, topic here is on data format in a GridView object. To get the proper data format shown in GridView, we should apply the appropriate data formatting expression to them. The data formatting expression consists of an optional literal part and a format specification part that looks like:

{0:format specifier}

where 0 is the Parameter Index indicating the first (and only) data element to be formatted; format specifier is self-explanatory. Examples are given below (via):

Data Formatting Expression
Applied to Types
Description
Price: {0:C}
numeric/decimal
“Price:” is shown, followed by the number formatted as currency.
{0:D4}
integer
Shows an integer in a zero-filled 4-char-long string format.
{0:N2}%
numeric
Accuracy is set to 2 digits after decimal point. Shown with a percentage sign.
{0:000.0}
numeric/decimal
Show 1 digit after decimal point. Zeroes are filled in if the number is less than 100.
{0:D}
date/datetime
Standard DateTime format(e.g. Saturday, November 08, 2008”).
{0:d}
date/datetime
Short DateTime string like 11/06/08.
{0:yy-MM-dd}
date/datetime
08-11-08.

Property for Data BoundField in GridView – DataFormatString

Usage: DataFormatString = “{0: format specifier}”

aspx code:

<asp:GridView ID="grvOrder" runat="server" AutoGenerateColumns="False" Width="100%">
    <Columns>
        <asp:BoundField HeaderText="ETA" DataField="OperationDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
        </asp:BoundField>
            <asp:BoundField HeaderText="Total" DataField="TotalRate" DataFormatString="{0:C}" HtmlEncode="False">
        </asp:BoundField>
    </Columns>
</asp:GridView>

Notice: Special attention should be paid to the “HtmlEncode” property. With its default value set to “True”, the DataFormatString will be invalid! Set it to “False” to make DataFormatString work.


已发布

分类

来自

评论

《“DataFormatString in GridView”》 有 1 条评论

  1. diesel t shirts 的头像

    Certainly believe that which you stated. Your favorite reason seemed to be at the web the easiest thing to consider associated with. I explain to you, I definitely get irked even while folks think about issues that they just dont understand about. You were able to hit the nail upon the very best as well as defined out the whole thing with no need side-effect , folks could take an indication. Will likely be again to get more. Thanks

回复 diesel t shirts 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注