How to add rows, items and subitems in a listview

This c# code shows you, how to add a row in a listview:

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

This creates 3 items and 3 subitems in a listview:

// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("Item 1", 0);
item1.SubItems.Add("Item1 Subitem 1");
item1.SubItems.Add("Item1 Subitem 2");
item1.SubItems.Add("Item1 Subitem 3");
ListViewItem item2 = new ListViewItem("Item 2", 1);
item2.SubItems.Add("Item2 Subitem 1");
item2.SubItems.Add("Item2 Subitem 2");
item2.SubItems.Add("Item2 Subitem 3");
ListViewItem item3 = new ListViewItem("Item 3", 1);
item3.SubItems.Add("Item3 Subitem 1");
item3.SubItems.Add("Item3 Subitem 2");
item3.SubItems.Add("Item3 Subitem 3");

//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });

or

// add a row with an item
listView1.Items.Add (new ListViewItem ("Row 1"));

or

// add a row with an item and 3 subitems in a line
listView1.Items.Add(new ListViewItem(new string[] { "Item1", "Sub1", "Sub2", "Sub3" }));

Ersten Kommentar schreiben

Antworten

Deine E-Mail-Adresse wird nicht veröffentlicht.


*


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.