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 Assignment
s 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();
}