C#: How to add subitems in ListView

后端 未结 10 1257
眼角桃花
眼角桃花 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: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);
    }
    

提交回复
热议问题