【问题标题】:C#: How to add subitems in ListViewC#:如何在 ListView 中添加子项
【发布时间】:2010-10-18 06:19:27
【问题描述】:

创建一个item(在key下)很简单,但是如何添加subitems(Value)呢?

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

【问题讨论】:

  • 试试这个。 listView1.Items.Add(new ListViewItem(new[] { "1", "2", "3", "4", "5" }));

标签: c# .net winforms list view


【解决方案1】:
ListViewItem item = new ListViewItem();
item.Text = "fdfdfd";
item.SubItems.Add ("melp");
listView.Items.Add(item);

【讨论】:

  • 什么是ListItem,编译器无法识别。
  • 我确定他的意思是 ListViewItem。
  • Listviewitem offcourse,很抱歉打错了,但我相信你应该自己发现它是 ListViewItem 而不是 ListItem。查看 MSDN 和 Listview 的 Items 属性的重载 Add 方法 ...
【解决方案2】:

您将子项放入一个数组并将该数组添加为列表项。

向数组中添加值的顺序决定了它们出现在哪个列下,因此可以将子项目标题视为 [0]、[1]、[2] 等。

这是一个代码示例:

//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);
}

【讨论】:

  • 呃...在过去的两周里,我使用了这种技术大约四次。为什么投反对票?
  • +1 为其他答案提供了一个完全有效的替代方案。我不知道为什么你收到了反对票。
  • 一个更简单的选择是这样做:listView.Items.Add(new ListViewItem(new string[]{"Col1", "SubItem2", "SubItem3", "And so on"}));
【解决方案3】:

创建一个列表视图项

ListViewItem item1 = new ListViewItem("sdasdasdasd", 0)
item1.SubItems.Add("asdasdasd")

【讨论】:

    【解决方案4】:

    像这样:

    ListViewItem lvi = new ListViewItem();
    lvi.SubItems.Add("SubItem");
    listView1.Items.Add(lvi);
    

    【讨论】:

      【解决方案5】:

      假设您有一个 List Collection,其中包含许多要在 ListView 中显示的项目,请看以下遍历 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);
        }
      

      该代码在 ListView 中生成以下输出(当然取决于您在 List 集合中有多少项):

      基本上第一列是一个包含许多子项(其他列)的列表视图项。可能看起来很奇怪,但是listview非常灵活,你甚至可以用它构建一个类似windows的文件浏览器!

      【讨论】:

      • 这个逻辑应该在Inspection类下,因为它必须知道所有的内部......
      【解决方案6】:

      太棒了!!它对我帮助很大。我曾经使用 VB6 做同样的事情,但现在完全不同了。 我们应该添加这个

      listView1.View = System.Windows.Forms.View.Details;
      listView1.GridLines = true; 
      listView1.FullRowSelect = true;
      

      【讨论】:

        【解决方案7】:

        我使用 ListViewItemsCollection 上的扩展方法对此进行了改进。在我看来,它使调用代码更简洁,也促进了更普遍的重用。

        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);
            }
        }
        

        调用 AddWithTextAndSubItems 如下所示:

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

        希望这会有所帮助!

        【讨论】:

        • 非常好,但是调用这个扩展方法的时候,作为 ListView.ListViewItemCollection 传递了什么?
        • @ClayShannon:因为它是一种扩展方法,它“扩展”了 .NET FX ListView.ListViewItemCollection 的行为。编译器将扩​​展方法样式转换为对静态方法的调用。所以myListViewControl.Items.AddWithTextAndSubItems("Text", "Sub Item 1", "Sub Item 2"); 变成了ListViewItemCollectionExtender.AddWithTextAndSubItems(myListViewControl.Items, "Text", "Sub Item 1", "Sub Item 2"); 编译魔法!!
        【解决方案8】:

        添加:

        .SubItems.Add("asdasdasd");
        

        到你的代码的最后一行,所以它最终看起来像这样。

        listView1.Items.Add("sdasdasdasd").SubItems.Add("asdasdasd");
        

        【讨论】:

          【解决方案9】:

          一般:

          ListViewItem item = new ListViewItem("Column1Text")
             { Tag = optionalRefToSourceObject };
          
          item.SubItems.Add("Column2Text");
          item.SubItems.Add("Column3Text");
          myListView.Items.Add(item);
          

          【讨论】:

            【解决方案10】:

            我认为最快/最简洁的方法:

            对于每个类都有string[] obj.ToListViewItem()方法然后这样做:

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

            这是一个示例定义

            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()
                    };
                }
            }
            

            作为一个额外的好处,你可以有一个 static 方法返回 ColumnHeader[] 列表,用于设置列表视图列

            listView1.Columns.AddRange(Person.ListViewHeaders());
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-07-10
              相关资源
              最近更新 更多