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.
<style type="text/css">
font-family:arial;
}
.gridHeader th{
border-bottom:dashed 1px #c0c0c0;
</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, "FirstName")%><%
</ItemTemplate>
<RowStyle CssClass="gridRow" />
</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
Currently rated 5.0 by 1 people
Tags: asp.net 2.0, c#, asp.net gridview, asp.net gridview dataformatstring date, asp.net string format function, c# string format, c# convert number to currency, asp.net gridview date, asp.net gridview dataformatstring, asp.net datetime format, c# datetime
8/23/2008 8:45:17 PM