C#: How to add subitems in ListView

后端 未结 10 1212
眼角桃花
眼角桃花 2020-11-29 07:33

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         


        
相关标签:
10条回答
  • 2020-11-29 07:56
    ListViewItem item = new ListViewItem();
    item.Text = "fdfdfd";
    item.SubItems.Add ("melp");
    listView.Items.Add(item);
    
    0 讨论(0)
  • 2020-11-29 07:59

    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("ffffdd dd/MM/yyyy - HH:mm:ss");
    
         ListViewItem lvi = new ListViewItem(saLvwItem);
    
         lvwMyListView.Items.Add(lvi);
    }
    
    0 讨论(0)
  • 2020-11-29 07:59

    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):

    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!

    0 讨论(0)
  • 2020-11-29 08:02

    add:

    .SubItems.Add("asdasdasd");
    

    to the last line of your code so it will look like this in the end.

    listView1.Items.Add("sdasdasdasd").SubItems.Add("asdasdasd");
    
    0 讨论(0)
提交回复
热议问题