C#: How to add subitems in ListView

You whack the subitems into an array and add the array as a list item.

The order in which you add values to the array dictates the column they appear under so think of your sub item headings as [0],[1],[2] etc.

Here’s a code sample:

//In this example an array of three items is added to a three column listview
string[] saLvwItem = new string[3];

foreach (string wholeitem in listofitems)
{
     saLvwItem[0] = "Status Message";
     saLvwItem[1] = wholeitem;
     saLvwItem[2] = DateTime.Now.ToString("dddd dd/MM/yyyy - HH:mm:ss");

     ListViewItem lvi = new ListViewItem(saLvwItem);

     lvwMyListView.Items.Add(lvi);
}

Leave a Comment