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

时间:2021-08-27 07:56:05

To add items to column 1 in my listView control (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列等?

7 个解决方案

#1


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

#2


You can add items / sub-items to 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:

但我怀疑你的问题是视图类型。在设计器中将其设置为Details或在代码中执行以下操作:

listView1.View = View.Details;

#3


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

        }

#4


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:

但正如詹姆斯·阿特金森所说,这只是将一个子项目添加到listviewitem中的问题:

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

#5


For your problem use like this:

对于您的问题使用如下:

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

#6


One line that i've made and it works:

我制作的一条线,它的工作原理:

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

#7


Use ListViewSubItem - See: MSDN

使用ListViewSubItem - 请参阅:MSDN

#1


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

#2


You can add items / sub-items to 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:

但我怀疑你的问题是视图类型。在设计器中将其设置为Details或在代码中执行以下操作:

listView1.View = View.Details;

#3


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

        }

#4


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:

但正如詹姆斯·阿特金森所说,这只是将一个子项目添加到listviewitem中的问题:

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

#5


For your problem use like this:

对于您的问题使用如下:

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

#6


One line that i've made and it works:

我制作的一条线,它的工作原理:

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

#7


Use ListViewSubItem - See: MSDN

使用ListViewSubItem - 请参阅:MSDN