C# listView,如何将项目添加到第 2、3 和 4 列等?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/473148/
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 04:49:01  来源:igfitidea点击:

C# listView, how do I add items to columns 2, 3 and 4 etc?

c#winformslistview

提问by

To add items to column 1 in my listViewcontrol (Winform) I'm using listView1.Items.Add, this works fine but how do I add items to columns 2 and 3 etc?

要将项目添加到我正在使用的listView控件 ( Winform) 中的第1 列listView1.Items.Add,这很好用,但是如何将项目添加到第 2 列和第 3 列等?

回答by Inisheer

There are several ways to do it, but here is one solution (for 4 columns).

有几种方法可以做到这一点,但这里是一种解决方案(4 列)。

string[] row1 = { "s1", "s2", "s3" };
listView1.Items.Add("Column1Text").SubItems.AddRange(row1);

And a more verbose way is here:

更详细的方法在这里:

ListViewItem item1 = new ListViewItem("Something");
item1.SubItems.Add("SubItem1a");
item1.SubItems.Add("SubItem1b");
item1.SubItems.Add("SubItem1c");

ListViewItem item2 = new ListViewItem("Something2");
item2.SubItems.Add("SubItem2a");
item2.SubItems.Add("SubItem2b");
item2.SubItems.Add("SubItem2c");

ListViewItem item3 = new ListViewItem("Something3");
item3.SubItems.Add("SubItem3a");
item3.SubItems.Add("SubItem3b");
item3.SubItems.Add("SubItem3c");

ListView1.Items.AddRange(new ListViewItem[] {item1,item2,item3});

回答by Jan Bannister

Use ListViewSubItem - See: MSDN

使用 ListViewSubItem - 请参阅:MSDN

回答by CalvinR

Here is the msdn documentation on the listview object and the listviewItem object.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.aspx

这是有关 listview 对象和 listviewItem 对象的 msdn 文档。
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.aspx

I would highly recommend that you at least take the time to skim the documentation on any objects you use from the .net framework. While the documentation can be pretty poor at some times it is still invaluable especially when you run into situations like this.

我强烈建议您至少花时间浏览有关您从 .net 框架使用的任何对象的文档。虽然文档有时可能非常糟糕,但它仍然是无价的,尤其是当您遇到这样的情况时。

But as James Atkinson said it's simply a matter of adding subitems to a listviewitem like so:

但正如 James Atkinson 所说,这只是将子项添加到 listviewitem 的问题,如下所示:

ListViewItem i = new ListViewItem("column1");
i.SubItems.Add("column2");
i.SubItems.Add("column3");

回答by bruno conde

You can add items/ sub-itemsto the ListView like:

您可以向 ListView添加项目/子项目,例如:

ListViewItem item = new ListViewItem(new []{"1","2","3","4"});
listView1.Items.Add(item);

But I suspect your problem is with the View Type. Set it in the designer to Details or do the following in code:

但我怀疑您的问题出在View Type 上。在设计器中将其设置为 Details 或在代码中执行以下操作:

listView1.View = View.Details;

回答by OneM

For your problem use like this:

对于您的问题,请使用以下方法:

ListViewItem row = new ListViewItem(); 
row.SubItems.Add(value.ToString()); 
listview1.Items.Add(row);

回答by ken4ward

 private void MainTimesheetForm_Load(object sender, EventArgs e)
        {
            ListViewItem newList = new ListViewItem("1");
            newList.SubItems.Add("2");
            newList.SubItems.Add(DateTime.Now.ToLongTimeString());
            newList.SubItems.Add("3");
            newList.SubItems.Add("4");
            newList.SubItems.Add("5");
            newList.SubItems.Add("6");
            listViewTimeSheet.Items.Add(newList);

        }

回答by David Rodrigues

One line that i've made and it works:

我制作的一行代码有效:

listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = randomArray["maintext"], SubItems = { randomArray["columntext2"], randomArray["columntext3"] } });