Introduction
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",
"Yesterday",
"Today",
"Tomorrow",
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday",
"Next week", "In 2 weeks",
"In 3 weeks", "In 4 weeks", "Next month",
"Newer"};
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)
{
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;
if (ts == 1)
return -1;
if (ts == -1)
return 1;
if ((ts > 1) && (ts <= dtRefWeekDay))
return dtWeekDay - 8;
if ((-ts > 1) && (-ts <= 6 - dtRefWeekDay))
return dtWeekDay + 2;
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);
else
return -13;
if ((-ts > 6 - dtRefWeekDay + nIdx * 7) &&
(-ts <= 6 - dtRefWeekDay + (nIdx + 1) * 7))
if (dtRef.Month == dtSource.Month)
return (9 + nIdx);
else
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 " +
dtDate.Date.ToString("dd/MM/yyyy"));
item.Group = listView1.Groups[GetDateTimeGroupIndex(DateTime.Now, dtDate) + 14];
listView1.Items.Add(item);
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.
History
- 04/14/2009: First submission.
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 (http://www.prometeo.fr)