2

I have a ListView with three columns which I want to add strings to each

This is the code

ListViewItem tempLV = new ListViewItem("first");
tempLV.SubItems.Add("second");
tempLV.SubItems.Add("third");
lv.Items.Add(tempLV);

and the output

enter image description here

As you can see only data from the first column is added and surrounded by ListViewItem:{}

The ListView comes from System.Windows.Controls,

and the ListViewItem comes from System.Windows.Forms, if that helps

using .NET 4.5

  • 3
    Let me get this straight. You mix the WPF ListView with WinForms ListViewItem? – Steve Aug 02 '13 at 20:36
  • Yes, because the tutorials I see the ListViewItem constructor has many overloads where you can pass an array of strings which would be great, but the WPF ListViewItem doesn't have any overloads, and I don't see a subItems property. – DrJonOsterman Aug 02 '13 at 20:50
  • I am not an expert on WPF but I a bit sceptics that this idea could work. (As you can see from the output) However I am waiting to be disavowed – Steve Aug 02 '13 at 21:03
  • 3
    I guess you don't have to be an expert on WPF to see that this approach is complete nonsense. Just because the WPF ListViewItem has no Subitems property can't make you use the WinForms one. These are two completely different UI frameworks that don't have anything in common except that both are based on .Net. The output you see is simply from the (WinForms) ListViewItem`s ToString method. This happens because you can put any .Net object into the WPF ListView's Items collection, even a WinForms ListViewItem. – Clemens Aug 02 '13 at 21:39
  • There are a lot of how-tos in the [ListView documentation](http://msdn.microsoft.com/en-us/library/system.windows.controls.listview.aspx) on MSDN. You may pay special attention to those mentioning `GridView`. – Clemens Aug 02 '13 at 21:49
  • Fully agree with @Clemens. Please remove all references to `System.Windows.Forms` for all your code immediately, and post a screenshot of what you need so we can tell you the proper way to do it in WPF. – Federico Berasategui Aug 02 '13 at 22:20
  • 2
    @Clemens edited that, I'm sorry. I just lost it when I saw what this guy is doing here. – Federico Berasategui Aug 02 '13 at 22:25

2 Answers2

-2

You are creating an instance of ListViewItem named "tempLV", and assigning that the "first" value. Then you are adding the next to ListViewItems AS SUBITEMS.

You need to create the ListViewItem like this:

var lvi = new ListViewItem();
lvi.SubItems.Add("first");
lvi.SubItems.Add("second");
lvi.SubItems.Add("third");

ListView lv = new ListView();
lv.Items.Add(lvi);
ganders
  • 7,285
  • 17
  • 66
  • 114
-3

If the requirement is to add three listview items, three instances of listview items should be made. SubItems won't work in this case

Arushi Agrawal
  • 619
  • 3
  • 10