119

To add items to column 1 in my listView control (Winform) I'm using listView1.Items.Add, this works fine but how do I add items to columns 2 and 3 etc?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243

6 Answers6

148

There are several ways to do it, but here is one solution (for 4 columns).

string[] row1 = { "s1", "s2", "s3" };
listView1.Items.Add("Column1Text").SubItems.AddRange(row1);

And a more verbose way is here:

ListViewItem item1 = new ListViewItem("Something");
item1.SubItems.Add("SubItem1a");
item1.SubItems.Add("SubItem1b");
item1.SubItems.Add("SubItem1c");

ListViewItem item2 = new ListViewItem("Something2");
item2.SubItems.Add("SubItem2a");
item2.SubItems.Add("SubItem2b");
item2.SubItems.Add("SubItem2c");

ListViewItem item3 = new ListViewItem("Something3");
item3.SubItems.Add("SubItem3a");
item3.SubItems.Add("SubItem3b");
item3.SubItems.Add("SubItem3c");

ListView1.Items.AddRange(new ListViewItem[] {item1,item2,item3});
Dayan
  • 7,634
  • 11
  • 49
  • 76
Inisheer
  • 20,376
  • 9
  • 50
  • 82
  • in my case, I added a subitem, but still, nothing appears, then I understood that I needed to add an additional header to the list view -> ColumnHeader header2 = new ColumnHeader { Text = "Column2", Name = "col2" }; listView.Columns.Add(header2); – Sirop4ik Jan 26 '21 at 08:07
  • For those looking to translate to Powershell `$lvi = New-Object Windows.Forms.ListViewItem($rowContentArr[0]); $lvi.SubItems.Add($rowContentArr[1]); $lvi.SubItems.Add("FALSE"); $lvwBtnRenameGroupsOutput.Items.Add($lvi);` – Bbb May 22 '23 at 19:45
73

You can add items / sub-items to the ListView like:

ListViewItem item = new ListViewItem(new []{"1","2","3","4"});
listView1.Items.Add(item);

But I suspect your problem is with the View Type. Set it in the designer to Details or do the following in code:

listView1.View = View.Details;
bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • 3
    It's very important also to use "listView1.FullRowSelect = true"... otherwise only column 1 is clickable. I wish I'd known this earlier! it would have saved me a ton of work. – John Henckel Jun 01 '15 at 15:18
  • Esspecially the remark about listView1.View did help me! Thnx! – Dev.Jaap Apr 11 '18 at 10:00
8

Here is the msdn documentation on the listview object and the listviewItem object.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.aspx

I would highly recommend that you at least take the time to skim the documentation on any objects you use from the .net framework. While the documentation can be pretty poor at some times it is still invaluable especially when you run into situations like this.

But as James Atkinson said it's simply a matter of adding subitems to a listviewitem like so:

ListViewItem i = new ListViewItem("column1");
i.SubItems.Add("column2");
i.SubItems.Add("column3");
lsalamon
  • 7,998
  • 6
  • 50
  • 63
CalvinR
  • 744
  • 1
  • 5
  • 11
7
 private void MainTimesheetForm_Load(object sender, EventArgs e)
        {
            ListViewItem newList = new ListViewItem("1");
            newList.SubItems.Add("2");
            newList.SubItems.Add(DateTime.Now.ToLongTimeString());
            newList.SubItems.Add("3");
            newList.SubItems.Add("4");
            newList.SubItems.Add("5");
            newList.SubItems.Add("6");
            listViewTimeSheet.Items.Add(newList);

        }
ken4ward
  • 2,246
  • 5
  • 49
  • 89
4

For your problem use like this:

ListViewItem row = new ListViewItem(); 
row.SubItems.Add(value.ToString()); 
listview1.Items.Add(row);
Jared Harley
  • 8,219
  • 4
  • 39
  • 48
OneM
  • 51
  • 2
-4

Use ListViewSubItem - See: MSDN

CJM
  • 11,908
  • 20
  • 77
  • 115
Jan Bannister
  • 4,859
  • 8
  • 38
  • 45