2

I am creating a winforms application in visual studio 2017, I am populating a ListView using a

 List<KeyValuePair<string, string>>

Examples of the data are:

 List<KeyValuePair<ABC, 123>>
 List<KeyValuePair<ABC, 456>>
 List<KeyValuePair<ABC, 789>>
 List<KeyValuePair<DEF, 123>>
 List<KeyValuePair<DEF, 233>>

I try to diplay this in a ListView, where I would like to have sometihng like this:

  • ABC

    • 123
    • 456
    • 789
  • DEF

    • 123
    • 233

Where the ABC and the DEF are selectable only. I try to write a code to do this, but unfortunately it only displays the ABC and DEF without the subitems.

The code I wrote is:

         workOrderClusters = GetItac.FilterWorkOrderClusters();
        // GetItac.FilterWorkOrderClusters() is a  
        List<KeyValuePair<string,string>>
        string current; string previous,
        foreach (var workOrderCluster in workOrderClusters)
        {
            current = workOrderCluster.Key;
            if (current != previous)
            {
                var listViewItem = new ListViewItem(workOrderCluster.Key);
                foreach (var cluster in workOrderClusters)
                {
                    if (cluster.Key == current)
                    {
                        listViewItem.SubItems.Add(cluster.Value);
                    }
                }
            }
            previous = current;
            listView1.Items.Add(listViewItem);

My question is, is there anyway to make the ListView display as expected ?

Omar AlMadani
  • 161
  • 2
  • 12
  • That looks like a TreeView. – LarsTech Jun 12 '18 at 14:37
  • @LarsTech is this not possible to do ? – Omar AlMadani Jun 12 '18 at 14:39
  • Not sure why you are using a ListView. Either way, this is a debugging problem. Put a debug stop on your code and run it, when it stops, step through the code line by line to see what happens and inspect the values. You will see why your subitems are never being added. – LarsTech Jun 12 '18 at 14:40
  • @LarsTech I have debugged the code actually, it adds the SubItems but it never displays them inside the ListView. – Omar AlMadani Jun 12 '18 at 14:50
  • Also make sure you are use details view and you have some columns. – Reza Aghaei Jun 12 '18 at 14:53
  • @RezaAghaei I am not familiar with the details views and colums in winforms, could you tell me some details please ans some examples if possible – Omar AlMadani Jun 12 '18 at 14:55
  • For two levels only a ListView with groups is perfect! – TaW Jun 12 '18 at 17:59
  • While group may be useful for some cases, but in this case if the user wants to take some action based on selected item (group?), IMO using group is not much user-friendly. – Reza Aghaei Jun 12 '18 at 19:29

1 Answers1

2

ListView shows sub items if it's in Details view and it has some columns.

Let's say you have the following data:

var list = new List<KeyValuePair<string, int>>(){
    new KeyValuePair<string, int>("ABC", 123),
    new KeyValuePair<string, int>("ABC", 456),
    new KeyValuePair<string, int>("ABC", 789),
    new KeyValuePair<string, int>("DEF", 123),
    new KeyValuePair<string, int>("DEF", 233),
};

To convert your data structure to ListView items you can first group data based on the key:

var data = list.GroupBy(x => x.Key).Select(x => new
    {
        Key = x.Key,
        Values = x.Select(a => a.Value)
    });

Then add items and sub items to the control:

foreach(var d in data)
{
    var item = listView1.Items.Add(d.Key);
    foreach (var v in d.Values)
        item.SubItems.Add(v.ToString());
}

And then setup ListView to show them:

listView1.View = View.Details;
var count = data.Max(x => x.Values.Count());
for (int i = 0; i <= count; i++)
    listView1.Columns.Add($"Column {i+1}");

Note

As also mentioned in the comments, probably TreeView is more suitable to show such data. In case you want to add that data to TreeView, after grouping data you can use the following code:

foreach (var d in data)
{
    var node = treeView1.Nodes.Add(d.Key);
    foreach (var v in d.Values)
        node.Nodes.Add(v.ToString());
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 2
    I think it is worth mentioning that ListView can actually group its Items. – TaW Jun 12 '18 at 15:52
  • Yes and it's up to the OP/future readers to discover more about ListView groups. – Reza Aghaei Jun 12 '18 at 16:04
  • @RezaAghaei Is it possible to display the tree nodes without the plus or minus ? just the tree nodes with its nodes ? and is it possible to make just the header of the tree selectable ? – Omar AlMadani Jun 13 '18 at 09:41
  • 1
    @OmarAlMadani Yes, you can set `ShowPlusMinus` to `false`. But users still can collapse or expand by double click or keyboard. If you want to prevent collapse and expand, you need to handle corresponding events. – Reza Aghaei Jun 13 '18 at 10:15