ASP.Net Gridview DataFormatString Date Format

by top54u.com 13 Jul, 2008
Spotlight.....

ASP.Net GridView Data Control provides the DataFormatString property in BoundCoulmn that allows you to format the date, strings and conversion of decimal number system into other number system or currency format. You can format the Date into different formats e.g.: MMM dd, yyyy, MM-dd-yyyy, MM/dd/yyyy, dd/MM/yyyyy etc as per your requirement. You can learn about number formats in the previous article about ASP.Net GridView DataFormatString. In this tutorial we have used the employees table of Northwind SQL Database to explain the use of DataFormatString property for GridView Date. BirthDate field of employees table is displayed with different date formats using string format syntax in DataFormatString property of BoundColumn. You can set the GridView Date Formats such as: {0:MM-dd-yyyy} to display the date with month number, day and year, {0:MMM dd, yyyy} to display the date with first three letters of month name, day and year.

 

Code for ASP.Net GridView DataFormatString Date Format

 

<style type="text/css">

body {

font-family:arial;

font-size:12px;

}

 

.gridHeader th{

border-bottom:solid 2px #cccccc;

}

 

.gridRow td{

border-bottom:dashed 1px #c0c0c0;

margin-top:5px;

}

</style>

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" CellPadding="4" CellSpacing="0" HeaderStyle-HorizontalAlign="Left" Width="400px" RowStyle-CssClass="gridRow" CssClass="gridHeader">

<Columns>

<asp:TemplateField HeaderText="Employee Name">

<ItemTemplate>

<%#DataBinder.Eval(Container.DataItem, "TitleOfCourtesy")%>

<%#DataBinder.Eval(Container.DataItem, "FirstName")%><%

#DataBinder.Eval(Container.DataItem, "LastName")%>

</ItemTemplate>

</asp:TemplateField>

 

<asp:BoundField DataField="BirthDate" DataFormatString="{0:MM-dd-yyyy}" HtmlEncode="false" HeaderText="MM-dd-yyyy" />

 

<asp:BoundField DataField="BirthDate" DataFormatString="{0:MMM dd, yyyy}" HtmlEncode="false" HeaderText="MMM dd, yyyy" />

 

<asp:BoundField DataField="BirthDate" DataFormatString="{0:ddd MM, yyyy}" HtmlEncode="false" HeaderText="ddd MM, yyyy" />

 

</Columns>

 

<RowStyle CssClass="gridRow" />

<HeaderStyle HorizontalAlign="Left" />

</asp:GridView>

 

From the above example code of GridView Control having DataFormatString property for BoundColumn you can see that HtmlEncode property is used with value false coz DataFormat String property supports the string format syntax only if HtmlEncode="false".

 

Output:

 

 

You can learn the C# data format string syntax from the tutorial for C# Function to Get Current DateTime

 

You can download the C# code to bind GridView Control with Northwind SQL Database at:

ASP.Net GridView Highlight Row onmouseover

Spotlight.....

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , , ,

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 8:45:17 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....