CJ

The circle of life – ** .Net – Training – MOSS **

  • Past Post

  • Vistor Locations

GridView, DateTime, Display and Sorting

Posted by Clayton James on November 20, 2007

Ok, this is quite a simple solution but it took me a while to find it so I am going to blog about it (because I will forget it).

Requirements:
You have a DateTime column being displayed in a GridView control and you want to allow users the ability to sort the column by clicking on the column header, and you don’t want to display the time component of the data type.

No Good:
You can convert it to a varchar in SQL and format the data type to return only the date component but then this breaks the sorting functionality (sort by string instead of DateTime)

Solution:
You need to keep it as a DateTime format and set the following GridView column properties
dataformatstring=“{0:dd/MMM/yyyy}” htmlencode=“false”
This displays the Date without the time component and still sorts the column correctly 🙂

 Update:BrianB comment
A way to do this for the entire web application is to modify the global.asax Application_BeginRequest method.

System.Globalization.CultureInfo vNewCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
vNewCulture.DateTimeFormat.ShortDatePattern = “dd-MMM-yyyy”;
vNewCulture.DateTimeFormat.DateSeparator = “-“;
vNewCulture.DateTimeFormat.ShortTimePattern = “hh:mm”;  // add “tt” if you need AM/PM descriptors
vNewCulture.DateTimeFormat.LongTimePattern = “hh:mm:ss”
System.Threading.Thread.CurrentThread.CurrentCulture = vNewCulture;

I haven’t tested this as my original requirements only wanted the Date component (not the time) displayed but I am sure it would take much to get something working.

Advertisements

7 Responses to “GridView, DateTime, Display and Sorting”

  1. Brian B said

    Thanks for the post. I started using this code, but then I had to define all my columns rather than letting them autogenerate. I found this post which shows you how to change the Culture properties on a page for all date formatting. I thought this might be helpful to others.

    http://forums.asp.net/p/1223734/2190227.aspx#2190227

  2. Preeti said

    I have used the dataformatstring=“{0:g}” htmlencode=“false”
    However, i want this to be sorted in the desc order as well.
    Could you help me?

  3. NK said

    Hi good solution

  4. nash said

    very nice and simple soln. I was doing quite opposite to your solution and banging head.
    Finally i got it thanks.

  5. Ahmad said

    Thanks alot for your nice post…

  6. Kaitlyn said

    In the year 2003, Sobig appeared as an email attachment in a computer, which could
    infect the host if downloaded and executed. Nearly 12% of women in the United States will be diagnosed with breast cancer. You may choose to disable such service, by choosing to ignore by default any such material posted.

  7. Tracy said

    GridView DateTime Column Custom

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: