5
\$\begingroup\$

I have written simple code which loops through a list object and create groups in ListView depending on the data in the list. I have a list object of type List<Assignment> where Assignment is a class as follows.

public class Assignment
{
    public TimeSpan Time { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
    public int ImageIndex { get; set; }
}

Now, the list object have several assignments with different TimeSpan. My job is to group together all the Assignments with similar TimeSpan into one group and add this group to the ListView. The following code works perfectly fine for me. Still, I would love to know if there is better way of doing this.

The list passed into the function is sorted by TimeSpan.

private void FeedDataToPane(List<Assignment> assignmentList)
{
    ListViewGroup _listViewGrp;
    ListViewItem _listViewItem;

    MonitorPane.BeginUpdate();
    MonitorPane.Groups.Clear();
    MonitorPane.SmallImageList = imageList;

    for (int i = 0; i < assignmentList.Count; i++)
    {
        if (i == 0)
        {
            _listViewGrp = new ListViewGroup(assignmentList[i].Time.ToString());
            MonitorPane.Groups.Add(_listViewGrp);

            _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
            _listViewItem.SubItems.Add(assignmentList[i].Description);
            MonitorPane.Items.Add(_listViewItem);
        }
        else
        {
            if (assignmentList[i].Time.Equals(assignmentList[i - 1].Time))
            {
                _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
                _listViewItem.SubItems.Add(assignmentList[i].Description);
                MonitorPane.Items.Add(_listViewItem);
            }
            else
            {
                _listViewGrp = new ListViewGroup(assignmentList[i].Time.ToString());
                MonitorPane.Groups.Add(_listViewGrp);

                _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
                _listViewItem.SubItems.Add(assignmentList[i].Description);
                MonitorPane.Items.Add(_listViewItem);
            }
        }
    }

    MonitorPane.EndUpdate();
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

It's possible with the help of LINQ to reduce your code to this (assuming, of course, the version of .NET you're using will allow it):

private void FeedDataToPane(IEnumerable<Assignment> assignmentList)
{
    MonitorPane.BeginUpdate();
    MonitorPane.Groups.Clear();
    MonitorPane.SmallImageList = imageList;

    assignmentList
        .GroupBy(assignment => assignment.Time, CreateListViewGroup)
        .ToList();

    MonitorPane.EndUpdate();
}

private static ListViewGroup CreateListViewGroup(TimeSpan time, IEnumerable<Assignment> assignments)
{
    var group = new ListViewGroup(time.ToString());
    MonitorPane.Groups.Add(group);

    foreach (var assignment in assignments)
    {
        var item = new ListViewItem(assignment.Type, assignment.ImageIndex, group);
        item.SubItems.Add(assignment.Description);
        MonitorPane.Items.Add(item);
    }

    return group;
}
\$\endgroup\$
8
  • \$\begingroup\$ Hi George, When I add this code of yours, the CreateListViewGroup is not getting called. \$\endgroup\$
    – Govs
    Jun 2, 2014 at 13:50
  • \$\begingroup\$ @Govs Forgot that GroupBy is deferred. Fixed. \$\endgroup\$ Jun 2, 2014 at 13:58
  • \$\begingroup\$ Downvoter, care to explain what is horrendously wrong with the answer? \$\endgroup\$ Jun 2, 2014 at 14:00
  • \$\begingroup\$ Cool, That works like a charm. Clean. I am going to accept this as answer. Who ever down voted please care to explain if you have a better answer. \$\endgroup\$
    – Govs
    Jun 2, 2014 at 14:07
  • \$\begingroup\$ Hi George, I searched online and read through some posts and blogs about GroupBy method. But I still don't fully understand how it works. I understand that it groups by time and then pass each group to the CreateListViewGroup method. But the method returns group back. How and where does this returned group gets handled? Do you have any link which can explain this? \$\endgroup\$
    – Govs
    Jun 2, 2014 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.