Data Formatting String – ASP.NET

There are many format strings available to format your data in desired format.

I found it easy to have a list of all applicable format strings at one place so I can apply without looking around searching what is going to fit best according to the requirement. I have compiled a list of formatting strings that work with ASP.NET GridView. Hope you find this table helpful.

Format String

Description

Example

Number Format

{0:C}

Local Currency Format

$13.09

{0:C0}

Local Currency Format without decimal points

$3

{0:C4}

Local Currency Format with 4(four) decimal points

$6.9223

{0:D}

Integer Number only

680

{0:D8}

Integer Number of 8 digits (if unavailable the padded with leading zero)

00000680

{0:E}

Scientific Format

1.059531E+004

{0:F}

Fixed Number format

38.49

{0:F4}

Fixed Number format with 4 decimal places

10595.3100

{0:N}

Number format with thousand separator

10,595.31

{0:P}

Number format with Percentage

34%

{0:X}

Hexadecimal format (Integer only)

2a8

Date Format

{0:d}

Small Date

3/11/2004

{0:D}

Long Date

Thursday, March 11, 2004

{0:f}

Long Date, Small Time

Thursday, March 11, 2004 10:01 AM

{0:F}

Long Date, Long Time

Thursday, March 11, 2004 10:01:36 AM

{0:g}

Small Date, Small Time

3/11/2004 10:01 AM

{0:G}

Small Date, Long Time

3/11/2004 10:01:36 AM

{0:m} or {0:M}

Month Date

March 11

{0:t}

Small Time

10:01 AM

{0:T}

Long Time

10:01:36 AM

{0:U}

Universal long date and long time

Thursday, March 11, 2004 3:01:36 PM

{0:y} or {0:Y}

Month Year

March, 2004

 

I used GridView with Bound field and Sql Data Source with connection to Adventure works database to test all these settings:

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

DataKeyNames="ProductID" DataSourceID="SqlDataSource1"

EmptyDataText="There are no data records to display.">

<Columns>

<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"

SortExpression="ProductID" DataFormatString="{0:x}" />

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

<asp:BoundField DataField="StandardCost" HeaderText="StandardCost"

SortExpression="StandardCost" DataFormatString="{0:P0}" />

<asp:BoundField DataField="ProductNumber" HeaderText="ProductNumber"

SortExpression="ProductNumber" />

<asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate"

SortExpression="ModifiedDate" DataFormatString="{0:y}" />

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:ConnStr %>"

ProviderName="<%$ ConnectionStrings:ConnStr.ProviderName %>"

SelectCommand="SELECT [ProductID], [Name], [StandardCost], [ProductNumber],ModifiedDate FROM [Product]"

>

</asp:SqlDataSource>

 

</div>

</form>

</body>

</html>

Tags:

ASP.NET