C#:如何在 ListView 中添加子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/729090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:50:12  来源:igfitidea点击:

C#: How to add subitems in ListView

c#.netwinformslistview

提问by Ivan Prodanov

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?

采纳答案by Marcus L

Like this:

像这样:

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

回答by Frederik Gheysels

ListViewItem item = new ListViewItem();
item.Text = "fdfdfd";
item.SubItems.Add ("melp");
listView.Items.Add(item);

回答by Frederik Gheysels

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.

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

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

回答by RvdK

Create a listview item

创建一个列表视图项

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

回答by jasonco

Suppose you have a List Collection containing many items to show in a ListView, take the following example that iterates through the List Collection:

假设您有一个 List 集合,其中包含许多要在 ListView 中显示的项目,请看以下遍历 List 集合的示例:

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

该代码在 ListView 中生成以下输出(当然取决于您在 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!

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

回答by Patrick

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

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

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

回答by BrianB

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.

我使用 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);
    }
}

Calling the AddWithTextAndSubItems looks like this:

调用 AddWithTextAndSubItems 看起来像这样:

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

Hope this helps!

希望这可以帮助!

回答by alpharedx

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

回答by Dave Cousineau

Generally:

一般来说:

ListViewItem item = new ListViewItem("Column1Text")
   { Tag = optionalRefToSourceObject };

item.SubItems.Add("Column2Text");
item.SubItems.Add("Column3Text");
myListView.Items.Add(item);

回答by ja72

I think the quickest/neatest way to do this:

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

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

对于每个类都有string[] obj.ToListViewItem()方法,然后执行以下操作:

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 staticmethod that returns ColumnHeader[]list for setting up the listview columns with

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

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