ASP.Net Gridview DataFormatString Date Format

Updated on 27 Jul 2012,
Published on 18 Jul 2008

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

CSS Code
<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>
HTML Code
<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:

Get the output of above discussed code from the following link:

GridView DataFormatString Date

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

Continue to next tutorial: ASP.Net Gridview Select Row to learn how to set a selected row in gridview control.

7 Responses to "ASP.Net Gridview DataFormatString Date Format"
ahmed ansari
i am using three dropdownlist for date date of birth Example (date, month, year) how to edit date, if person add his date of birth wrong how to edit date in asp.net c# 2.0
Ayaz Maqsood
Thanks buddy it worked for me :)
P P
Thanks !
mohamed
its very nice.....thank u
very helpfull..... thank you
instanceOf
nice, I solved my problem by reading this post. :)
ds
thanks buddy
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email