views:

25081

answers:

6

Hi,

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? It's the first time I've used listView.

Thanks

A: 

Use ListViewSubItem :)

Jan Bannister
+7  A: 

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

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

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

ListViewItem item3 = new ListViewItem("Somethin3");
item3.SubItems.Add("SubItem3a");
item3.SubItems.Add("SubItem3a");

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

This looks good...

Jason Punyon
+1  A: 

Opps forgot the link to MSDN

Jan Bannister
+2  A: 

Here is an article on how to use Listviews: http://www.ondotnet.com/pub/a/dotnet/2002/10/28/listview.html

and 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

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.

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

ListViewItem i = new ListViewItem("column1");
i.SubItems.Add("column2");
i.SubItems.Add("column3");
CalvinR
+5  A: 

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

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:

listView1.View = View.Details;
bruno conde