prelic prelic - 2 months ago 11
C# Question

Adding Columns and ListView items

This is my first foray into C# WinForms, and I'm trying to create a ListView grid.

My code looks like this:

listView1.Columns.Add("Name", 100);
listView1.Columns.Add("Col2", 200);
listView1.Columns.Add("Col3", 300);
string[] arr = new string[3];
arr[0] = "product_1";
arr[1] = "100";
arr[2] = "10";
string[] arr2 = new string[3];
arr[0] = "product_2";
arr[1] = "200";
arr[2] = "20";
ListViewItem itm = new ListViewItem(arr);
ListViewItem itm2 = new ListViewItem(arr2);

But the output looks like this:


So a few questions here:

  • Why do I not see column names?

  • Why do I not see grid lines?

  • Why is only the last entry being displayed (product2)?

  • Why is there no information other than the string "product2" displayed, rather than the other column entries?

Any help is greatly appreciated!


The default viewstyle of a ListView is LargeIcon. In that view, no columns or details are displayed. If you want your columns to be visible, you should set the View property of the ListView to Details:

listView1.View = View.Details;

On a sidenote: when you're adding lots of ListViewItems, it might be better to use the AddRange method instead of adding each item seperatly. That will significantly improve performance. Also, make use of the BeginUpdate() and EndUpdate() methods on the ListView. This will prevent the ListView to be redrawn with each add of a ListViewItem.