patterncsharpMinor
Adding groups to ListView depending on the data in a list
Viewed 0 times
groupstheaddingdependinglistdatalistview
Problem
I have written simple code which loops through a list object and create groups in
Now, the list object have several assignments with different
The list passed into the function is sorted by
```
private void FeedDataToPane(List 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(_
ListView depending on the data in the list. I have a list object of type List 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 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(_
Solution
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 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 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;
}Code Snippets
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;
}Context
StackExchange Code Review Q#52261, answer score: 2
Revisions (0)
No revisions yet.