In this article I will explain with an example, how to format DateTime field in dd/MM/yyyy format in ASP.Net GridView using C# and VB.Net.
This article will illustrate how to format DateTime field value to dd/MM/yyyy format in BoundField as well as TemplateField columns of ASP.Net GridView.
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Formatting DateTime field in dd/MM/yyyy format in BoundField column of ASP.Net GridView
The BoundField column of GridView provides a property named DataFormatString which can be used to format the Field value to the desired format.
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with three BoundField columns. The third BoundField column is bound to a DateTime field and the DataFormatString property is set to {0:dd/MM/yyyy} in order to display the DateTime field value in dd/MM/yyyy format.
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="BirthDate" HeaderText="Birth Date" ItemStyle-Width="150"
            DataFormatString="{0:dd/MM/yyyy}" />
    </Columns>
</asp:GridView>
 
Populating the GridView
Inside the Page Load event, the GridView is populated with a DataTable containing some dummy records.
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", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("BirthDate",typeof(DateTime)) });
        dt.Rows.Add(1, "John Hammond", new DateTime(1975, 12, 1));
        dt.Rows.Add(2, "Mudassar Khan", new DateTime(1985, 2, 25));
        dt.Rows.Add(3, "Suzanne Mathews", new DateTime(1992, 3, 23));
        dt.Rows.Add(4, "Robert Schidner", new DateTime(1982, 1, 15));
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("BirthDate", GetType(DateTime))})
        dt.Rows.Add(1, "John Hammond", New DateTime(1975, 12, 1))
        dt.Rows.Add(2, "Mudassar Khan", New DateTime(1985, 2, 25))
        dt.Rows.Add(3, "Suzanne Mathews", New DateTime(1992, 3, 23))
        dt.Rows.Add(4, "Robert Schidner", New DateTime(1982, 1, 15))
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Formatting DateTime field in dd/MM/yyyy format in TemplateField column of ASP.Net GridView
The TemplateField column makes use of the EVAL function for populating the data and hence the ToString function will be used to format the data in desired format.
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with two BoundField columns and one TemplateField column. The TemplateField column is bound to a DateTime field using the EVAL function.
The DateTime field value is converted to dd/MM/yyyy format by first converting the field value to DateTime object and later formatting it to dd/MM/yyyy format using the ToString function.
<asp:GridView ID="GridView2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Birth Date" ItemStyle-Width="150">
            <ItemTemplate>
                <%# Convert.ToDateTime(Eval("BirthDate")).ToString("dd/MM/yyyy") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Populating the GridView
Inside the Page Load event, the GridView is populated with a DataTable containing some dummy records.
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", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("BirthDate",typeof(DateTime)) });
        dt.Rows.Add(1, "John Hammond", new DateTime(1975, 12, 1));
        dt.Rows.Add(2, "Mudassar Khan", new DateTime(1985, 2, 25));
        dt.Rows.Add(3, "Suzanne Mathews", new DateTime(1992, 3, 23));
        dt.Rows.Add(4, "Robert Schidner", new DateTime(1982, 1, 15));
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("BirthDate", GetType(DateTime))})
        dt.Rows.Add(1, "John Hammond", New DateTime(1975, 12, 1))
        dt.Rows.Add(2, "Mudassar Khan", New DateTime(1985, 2, 25))
        dt.Rows.Add(3, "Suzanne Mathews", New DateTime(1992, 3, 23))
        dt.Rows.Add(4, "Robert Schidner", New DateTime(1982, 1, 15))
        GridView2.DataSource = dt
        GridView2.DataBind()
    End If
End Sub
 
 
Screenshot
Format DateTime field in dd/MM/yyyy format in ASP.Net GridView
 
 
Demo
 
 
Downloads