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:
GridView – For displaying data.
The GridView consists of 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="gvCustomers1" 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 handler, an object of DataTable is created.
Then, three columns are added to the DataTable Columns collection using the AddRange method.
An Array of objects of type DataColumn is specified which will hold the name and the optional parameter Data Type i.e. the Type of the column.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
Finally, the DataTable is assigned to the DataSource property of the GridView and the GridView is populated.
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));
        gvCustomers1.DataSource = dt;
        gvCustomers1.DataBind();
 
        gvCustomers2.DataSource = dt;
        gvCustomers2.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As ObjectByVal 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))
        gvCustomers1.DataSource = dt
        gvCustomers1.DataBind()
 
        gvCustomers2.DataSource = dt
        gvCustomers2.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:
GridView – For displaying data.
The GridView consists of two BoundField columns and one TemplateField column.
The TemplateField column consists of ItemTemplate which is bound with 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="gvCustomers2" 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 handler, an object of DataTable is created.
Then, three columns are added to the DataTable Columns collection using the AddRange method.
An Array of objects of type DataColumn is specified which will hold the name and the optional parameter Data Type i.e. the Type of the column.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
Finally, the DataTable is assigned to the DataSource property of the GridView and the GridView is populated.
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));
        gvCustomers1.DataSource = dt;
        gvCustomers1.DataBind();
 
        gvCustomers2.DataSource = dt;
        gvCustomers2.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As ObjectByVal 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))
        gvCustomers1.DataSource = dt
        gvCustomers1.DataBind()
 
        gvCustomers2.DataSource = dt
        gvCustomers2.DataBind()
    End If
End Sub
 
 

Screenshot

Format DateTime field in dd/MM/yyyy format in ASP.Net GridView
 
 

Demo

 
 

Downloads