In this article I will explain how to format DateTime in BoundField and TemplateField columns of ASP.Net GridView using C# and VB.Net.
This article explains how to format DateTime directly in BoundField column and also when Eval function is used to bind controls like Label, TextBox, etc. inside TemplateField columns.
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
I have created a dynamic DataTable with some dummy data and it is used to populate the GridView control in the Page Load event.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
Note: If you want to format DateTime, then you must always use DateTime data type for populating DateTime field in GridView.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("BirthDate", typeof(DateTime)) });
        dt.Rows.Add(1, "John Hammond", new DateTime(1977, 9, 12));
        dt.Rows.Add(2, "Mudassar Khan", new DateTime(1985, 2, 25));
        dt.Rows.Add(3, "Suzanne Mathews", new DateTime(1985, 5, 14));
        dt.Rows.Add(4, "Robert Schidner", new DateTime(1988, 10, 22));
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("BirthDate", GetType(DateTime))})
        dt.Rows.Add(1, "John Hammond", New DateTime(1977, 9, 12))
        dt.Rows.Add(2, "Mudassar Khan", New DateTime(1985, 2, 25))
        dt.Rows.Add(3, "Suzanne Mathews", New DateTime(1985, 5, 14))
        dt.Rows.Add(4, "Robert Schidner", New DateTime(1988, 10, 22))
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
1. Format DateTime in GridView BoundField column in ASP.Net
The following HTML Markup consists of an ASP.Net GridView with three BoundField columns. The third column displays Birth Date in dd/MM/yyyy date format using the DataFormatString property.
In the following example, the DataFormatString property is set to {0:dd/MM/yyyy} which displays the DateTime field in dd/MM/yyyy date format.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="100px" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120px" />
        <asp:BoundField DataField="BirthDate" HeaderText="Birth Date" DataFormatString = "{0:dd/MM/yyyy}" ItemStyle-Width="120px" />
    </Columns>
</asp:GridView>
 
Format DateTime in GridView BoundField and TemplateField columns in ASP.Net
 
 
2. Format DateTime in GridView TemplateField column in ASP.Net
The following HTML Markup consists of an ASP.Net GridView with two BoundField columns. The third column is a TemplateField column with a Label control and displays Birth Date in dd, MMM yyyy date format using the Eval function.
<asp:GridView ID="GridView1" runat = "server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="100px" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120px" />
        <asp:TemplateField HeaderText="Birth Date" ItemStyle-Width="120px" >
            <ItemTemplate>
                <asp:Label Text='<%# Eval("BirthDate", "{0:dd, MMM yyyy}") %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Format DateTime in GridView BoundField and TemplateField columns in ASP.Net
 
 
Demo
 
 
Downloads