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 ?

8
  • That looks like a TreeView. Commented Jun 12, 2018 at 14:37
  • @LarsTech is this not possible to do ? Commented Jun 12, 2018 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. Commented Jun 12, 2018 at 14:40
  • @LarsTech I have debugged the code actually, it adds the SubItems but it never displays them inside the ListView. Commented Jun 12, 2018 at 14:50
  • Also make sure you are use details view and you have some columns. Commented Jun 12, 2018 at 14:53

1 Answer 1

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()); } 
Sign up to request clarification or add additional context in comments.

4 Comments

I think it is worth mentioning that ListView can actually group its Items.
Yes and it's up to the OP/future readers to discover more about ListView groups.
@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 ?
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.