Click here to Skip to main content
13,201,050 members (68,850 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 14 Apr 2009

How to mimic Outlook group naming

, 14 Apr 2009
Rate this:
Please Sign up or sign in to vote.
How to group items in a listview based on a date.


Recently, I had to sort items in my listview. OK, I heard you, "nothing difficult doing that". But, I also had to group them as Outlook does. I mean "according to the date, today items, yesterday items, tomorrow items, next month items,...".

Using the Code

First of all, we have to define the label for each group:

private void Form1_Load(object sender, EventArgs e)
  string[] groupName = {"Older",
       "Last month", "4 weeks ago", "3 weeks ago", 
       "2 weeks ago", "Last week",
       "Monday", "Tuesday", "Wednesday", 
       "Thursday", "Friday", "Saturday", "Sunday",
       "Monday", "Tuesday", "Wednesday", 
       "Thursday", "Friday", "Saturday", "Sunday", 
       "Next week", "In 2 weeks", 
       "In 3 weeks", "In 4 weeks", "Next month", 

  // populate the group list
  foreach(string group in groupName)
    listView1.Groups.Add(new ListViewGroup(group));

Then, we have to define what group an item belongs to according to the current date and the item's date.

internal static int GetDateTimeGroupIndex(DateTime dtRef, DateTime dtSource)
  // Because week starts on Monday in France
  int dtWeekDay = (Convert.ToInt32(dtSource.DayOfWeek) + 6) % 7;
  int dtRefWeekDay = (Convert.ToInt32(dtRef.DayOfWeek) + 6) % 7;

  int ts = (dtRef.Date - dtSource.Date).Days;

  if (ts == 0)
    return 0;  // same day
  if (ts == 1)
    return -1; // day before

  if (ts == -1)
    return 1; // day after

  // same week 
  if ((ts > 1) && (ts <= dtRefWeekDay))
    return dtWeekDay - 8;

  if ((-ts > 1) && (-ts <= 6 - dtRefWeekDay))
    return dtWeekDay + 2;

  // previous / next weeks
  for (int nIdx = 0; nIdx <= 3; nIdx++)
    if ((ts > dtRefWeekDay + nIdx * 7) && 
        (ts <= dtRefWeekDay + (nIdx + 1) * 7))
      if (dtRef.Month == dtSource.Month)
       return -(9 + nIdx);
        return -13;
    if ((-ts > 6 - dtRefWeekDay + nIdx * 7) && 
        (-ts <= 6 - dtRefWeekDay + (nIdx + 1) * 7))
      if (dtRef.Month == dtSource.Month)
       return (9 + nIdx);
        return 13;

  if (Math.Abs(dtSource.Month - dtRef.Month) == 1) 
   return Math.Sign(ts) * (-13);
 return Math.Sign(ts) * (-14);

Finally, we have to set the Group property for every item as follows:

DateTime dtDate = DateTime.Now.AddMonths(-2);
while(dtDate < DateTime.Now.AddMonths(2))
  ListViewItem item = new ListViewItem("Item " + 
  item.Group = listView1.Groups[GetDateTimeGroupIndex(DateTime.Now, dtDate) + 14];
  dtDate = dtDate.AddDays(1);

And the result is ...

Points of Interest

The thing to note is that groups are displayed in the same order they are created; that is why I create them from older to newer and why I repeat the day name: the first time for when the item is older than today (if it is Friday, items from last Wednesday will belong to the group labeled "Wednesday" - the first one, and if it is Monday, items from next Wednesday will belong to the group labeled "Wednesday" too, but the second one).

Then, when the listview is sorted, groups are sorted, and in each group, items are sorted too.


  • 04/14/2009: First submission.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Crusty Applesniffer
Founder Prometeo Conseil
France France
When leaving the Ales School of Mines, my first employment, as project Manager and Trainer within company P2I Engineering, allowed me, all along these 7 years, developing my sense of contact and growing my technical knwoledge as well in project management as under software development.

It is strong of these experiments that I was then engaged, in 2001, by Symtrax, an international company (London, Boston, Los Angeles, Mumbaï, Nimes), publisher of software dedicated to analysis and data distribution. My main functions, initially, consisted in training the products manager to the project management like to methodology. Then, those quickly evolved to the management of the information system of the company, distributed on its 5 sites.

Then I was in charge of the management of the network (Proxy server, Mail servers, Active Directory server, Web servers, ftp - based on Windows (2000 and 2003) Operating Systems as well as Linux (RedHat) and SunOS - Solaris) of these 5 sites.

To manage Symtrax Information System, I set up existing tools (Nagios, Microsoft MOM 2005, SNORT…) and have developed my own solution (a solution for fighting against Spam on our Exchange server (in VB.NET 2005 and C#).

Moreover, I’ve managed the installation and the configuration of Microsoft CRM 3.0 on the Company’s sites.

This work implied a daily communication, in English, with the users of all the sites.

From march 2007 to end of 2008, at Data Health System, I was in charge of a new system that detect people’s fall: Ynolis.

From march 2009, I'm working as Consultant for Prometeo (

You may also be interested in...

Comments and Discussions

GeneralCode in Pin
mounaje210-Dec-10 1:01
membermounaje210-Dec-10 1:01 
QuestionHow about collapsing/folding group? Pin
Mamber14-Apr-09 21:19
memberMamber14-Apr-09 21:19 
AnswerRe: How about collapsing/folding group? Pin
Crusty Applesniffer14-Apr-09 23:03
memberCrusty Applesniffer14-Apr-09 23:03 
GeneralRe: How about collapsing/folding group? Pin
Mamber15-Apr-09 23:12
memberMamber15-Apr-09 23:12 
GeneralRe: How about collapsing/folding group? Pin
sameh_serag12-Aug-09 22:35
membersameh_serag12-Aug-09 22:35 
GeneralRe: How about collapsing/folding group? Pin
Crusty Applesniffer13-Aug-09 2:36
memberCrusty Applesniffer13-Aug-09 2:36 
GeneralMy vote of 4 Pin
xliqz14-Apr-09 9:29
memberxliqz14-Apr-09 9:29 
GeneralRe: My vote of 4 Pin
Crusty Applesniffer14-Apr-09 19:26
memberCrusty Applesniffer14-Apr-09 19:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 14 Apr 2009
Article Copyright 2009 by Crusty Applesniffer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid