Saturday 14 May 2016

Format DateTime column (field) in ASP.Net GridView with AutoGenerateColumns True

Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
Download and install Northwind Database
HTML Markup
The following HTML Markup consists of an ASP.Net GridView with AutoGenerateColumns property with set to True and specified with OnRowDataBound event.
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="OnRowDataBound">
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Binding the Repeater control using records form database
Inside the Page load event handler, the Repeater is populated with the records from the Customers table of the Northwind database.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        string query = "SELECT EmployeeId, (FirstName + '' + LastName) Name, BirthDate FROM Employees";
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgsHandles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub
Private Sub BindGrid()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Dim query As String = "SELECT EmployeeId, (FirstName + '' + LastName) Name, BirthDate FROM Employees"
        Using cmd As New SqlCommand(query)
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub
Formatting DateTime column (field) in ASP.Net GridView with AutoGenerateColumns True
Inside the OnRowDataBound event handler, the value of the DateTime field is formatted to the desired DateTime format.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[2].Text = Convert.ToDateTime(e.Row.Cells[2].Text).ToString("dd, MMM yyyy");
    }
}
VB.Net
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Cells(2).Text = Convert.ToDateTime(e.Row.Cells(2).Text).ToString("dd, MMM yyyy")
    End If
End Sub


No comments:

Post a Comment