C#: How to add subitems in ListView

后端 未结 10 1211
眼角桃花
眼角桃花 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:43

    Create a listview item

    ListViewItem item1 = new ListViewItem("sdasdasdasd", 0)
    item1.SubItems.Add("asdasdasd")
    
    0 讨论(0)
  • 2020-11-29 07:48

    Like this:

    ListViewItem lvi = new ListViewItem();
    lvi.SubItems.Add("SubItem");
    listView1.Items.Add(lvi);
    
    0 讨论(0)
  • 2020-11-29 07:49

    I think the quickest/neatest way to do this:

    For each class have string[] obj.ToListViewItem() method and then do this:

    foreach(var item in personList)
    {
        listView1.Items.Add(new ListViewItem(item.ToListViewItem()));
    }
    

    Here is an example definition

    public class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public DateTime DOB { get; set; }
        public uint ID { get; set; }
    
        public string[] ToListViewItem()
        {
            return new string[] {
                ID.ToString("000000"),
                Name,
                Address,
                DOB.ToShortDateString()
            };
        }
    }
    

    As an added bonus you can have a static method that returns ColumnHeader[] list for setting up the listview columns with

    listView1.Columns.AddRange(Person.ListViewHeaders());
    
    0 讨论(0)
  • 2020-11-29 07:51

    Generally:

    ListViewItem item = new ListViewItem("Column1Text")
       { Tag = optionalRefToSourceObject };
    
    item.SubItems.Add("Column2Text");
    item.SubItems.Add("Column3Text");
    myListView.Items.Add(item);
    
    0 讨论(0)
  • 2020-11-29 07:52

    I've refined this using an extension method on the ListViewItemsCollection. In my opinion it makes the calling code more concise and also promotes more general reuse.

    internal static class ListViewItemCollectionExtender
    {
        internal static void AddWithTextAndSubItems(
                                       this ListView.ListViewItemCollection col, 
                                       string text, params string[] subItems)
        {
            var item = new ListViewItem(text);
            foreach (var subItem in subItems)
            {
                item.SubItems.Add(subItem);
            }
            col.Add(item);
        }
    }
    

    Calling the AddWithTextAndSubItems looks like this:

    // can have many sub items as it's string array
    myListViewControl.Items.AddWithTextAndSubItems("Text", "Sub Item 1", "Sub Item 2"); 
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-29 07:55

    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 讨论(0)
提交回复
热议问题