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
Price: {0:C}
“Price:” is shown, followed by the number formatted as currency.
Shows an integer in a zero-filled 4-char-long string format.
Accuracy is set to 2 digits after decimal point. Shown with a percentage sign.
Show 1 digit after decimal point. Zeroes are filled in if the number is less than 100.
Standard DateTime format(e.g. Saturday, November 08, 2008”).
Short DateTime string like 11/06/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%">
        <asp:BoundField HeaderText="ETA" DataField="OperationDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
            <asp:BoundField HeaderText="Total" DataField="TotalRate" DataFormatString="{0:C}" HtmlEncode="False">

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


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