Wednesday, April 20, 2011

C#: How to add subitems in ListView

Creating an item(Under the key) is easy,but how to add subitems(Value)?

listView1.Columns.Add("Key");
listView1.Columns.Add("Value");
listView1.Items.Add("sdasdasdasd");
//How to add "asdasdasd" under value?

alt text

From stackoverflow
  • ListViewItem item = new ListItem();
    item.Text = "fdfdfd";
    item.SubItems.Add ("melp");
    listView.Items.Add(item);
    
    John : What is ListItem,the compiler doesn't recognise it.
    Marcus L : I'm sure he meant ListViewItem.
    Frederik Gheysels : Listviewitem offcourse, sorry for the typo, but with a little bit of effort I'm sure you should've found out yourself that it is ListViewItem instead of ListItem. Check out the MSDN and overloaded Add methods of the Items property of the 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);
    }
    
    Dan Tao : +1 for offering a perfectly valid alternative to the other answers. Why you received a downvote I do not know.
  • Create a listview item

    ListViewItem item1 = new ListViewItem("sdasdasdasd", 0)
    item1.SubItems.Add("asdasdasd")
    
    PoweRoy : why is this downvoted? Copy pasted it from msdn
  • Like this:

    ListViewItem lvi = new ListViewItem();
    lvi.SubItems.Add("SubItem");
    listView1.Items.Add(lvi);
    
  • Suppose you have a List Collection containing many items to show in a ListView, take the following example that iterates through the List Collection:

    foreach (Inspection inspection in anInspector.getInspections())
      {
        ListViewItem item = new ListViewItem();
        item.Text=anInspector.getInspectorName().ToString();
        item.SubItems.Add(inspection.getInspectionDate().ToShortDateString());
        item.SubItems.Add(inspection.getHouse().getAddress().ToString());
        item.SubItems.Add(inspection.getHouse().getValue().ToString("C"));
        listView1.Items.Add(item);
      }
    

    That code produces the following output in the ListView (of course depending how many items you have in the List Collection):

    alt text

    Basically the first column is a listviewitem containing many subitems (other columns). It may seem strange but listview is very flexible, you could even build a windows-like file explorer with it!

  • Great !! It has helped me a lot. I used to do the same using VB6 but now it is completely different. we should add this

    listView1.View = System.Windows.Forms.View.Details;

    listView1.GridLines = true;

    listView1.FullRowSelect = true;

0 comments:

Post a Comment