C#.Net GridView – How To: Format Datetime Column in Gridview in

In previous article we learned to bind GridView with SQLDataSource at Design Time and in Code behind. The article also explained what is GridView and why we need it?Now, in this article let’s learn how to format datetime column in gridview.

Why it is needed to format DateTime column in Gridview?

Suppose, you developed a web application which uses C# GridView control to display data on a web page retrieved from database. Actually, the GridView control doesn’t care about the columns that are returned with DataSource and it shows them on the page as it is. This is the default behavior of GridView in

If your DataSource is returned with few DateTime columns, then you can observe that the data shown in these columns will be in format “MM/DD/YYYY HH:MM:SS”. This is default format for DateTime column in GridView.

Now, what if you want to change the format of any columns? For ex. If you want to show the DateTime column in “MMMM d, yyyy”, then specifying this format in “DataFormatString” property of a GridView column will solve your problem.

You can change the format in two ways i.e. in Design time and/or another way is thru code behind. Let’s learn to format the DateTime column in both these ways.

How to format DateTime column in Gridview at design time?

We are assumung that your DataGrid is already configured with DataSource. We are considering here same GridView example taken in previous post in which output is as below –

GridView Output: Showing data from database
GridView Output: Showing data from database

Observe here “Birth_Date” column, by default the date values are showing in format “MM/DD/YYYY HH:MM:SS”. Yes, you can change the date format.

  1. To change the Date format click on the GridView and then click on arrow showing at right top of the GridView. It will open a dropdown window, select Edit columns from the list.

    GridView Edit Columns
    Gridview – Edit Columns
  2. A “Fields” window will open where you can customize the settings of all your columns. Now click on “Birth_Date” field, and enter “{0:MMMM d, yyyy}” in “DataFormatString” property as shown in below image. Now click on “Ok” button to finish.

    GridView - Properties of Fields
    GridView – Properties of Fields
  3. Now execute the application and see the output.

    GridView: format datetime column in gridview
    GridView: DateTime Formatted output

How to change the format of DateTime column in code behind?

  1. Open a page (.aspx) on which GridView control is placed.
  2. Go to Source of the page.

    ASP.Net open source page
    ASP.Net open source page
  3. Locate your GridView and find out a code of column for which a format needs to be changed. For example in my case, it would look like below –
    <asp:BoundField DataField=”Birth_Date”HeaderText=”Birth_Date”
  4. Now, add DataFormatString” property in above code. An example is given below, observe the highlighted part –
    <asp:BoundField DataField=”Birth_Date”HeaderText=”Birth_Date”
    SortExpression=”Birth_Date”DataFormatString=”&quot;{0:MMMM d, yyyy}&quot;” />
  5. Your C# GridView with improved DateTime column is now ready. Save the file and execute the application.

    GridView: DateTime Formatted output
    GridView: DateTime Formatted output

I hope this gridview example in helps you to format DateTime column in GridView. Please share the article on your online social network if you think is worth to share.

Following articles might be of your interest

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

4 Comments+ Add Comment

  • Rich content. Thanks

  • thanks it really solved my problem. :)

    • Hi Prags,
      I am glad that this post solved your problem. Thanks!

Leave a comment