Recently I had to design a dialog that will have some control in the form of ListBox with each item again having a CheckBox, two to three labels and one image. I was trying out some control and I came to know about the ListView control. In my last post, I was talking about the features that are provided by it. Now lets try to find out ways to use those properties.
Note – All the below mentioned features can be enabled using the Property window (F4), but I personally prefer the code way of doing it as I don’t like the auto generation part of VS (Visual Studio)
Adding a ListViewControl to the application
ListView control, like the other control can be added using the following snippet
private System.Windows.Forms.ListView myListView; //Decleration of a ListView control
this.Controls.Add(myListView); //Adding the control to the application
Changing the Display modes
In my last post I had posted some screenshots showing various display modes for the ListView control. We can set those using the View property of the ListView control.
myListView.View = System.Windows.Forms.View.SmallIcon; //Can be any mode (refer to the previous article to know about the modes)
Adding items in to the list (when the ListView is not under the Details view)
The following snippet will be able to add items to the ListView control if it is not under the Details view. Addition of items differ by Views because Details view will have columns (which will be discussed next). If the ListView control’s View is a normal, say SmallIcon view then we do it the following way.
//The following method has around 6 overloads. Now we will just consider the 2 one -- the string parameter
myListView.Items.Add("foo");
myListView.Items.Add("bar");
myListView.Items.Add("baz");
Adding Columns to the ListView
As I have already told, we need to create columns before adding Items in to the ListView control under the Details view. So here is the way to create columns in the ListView control.
//Adding columns to the ListView control
myListView.Columns.Add("S.No");
myListView.Columns.Add("FileName");
myListView.Columns.Add("FilePath");
Adding items in to the list (when the ListView is under the Details view)
After the addition of columns, we can add any number of items to the ListView control. An item in here will be categorised in to two — Parent and Children. Parent will be the item’s main field(ListViewItem), while the child will be the ListViewSubItem.
ListViewItem aFooItem = new ListViewItem("1"); //Parent item
ListViewItem.ListViewSubItem aSubFooItem1 = new ListViewItem.ListViewSubItem(aFooItem, "Foo"); //Creating subitems for the parent item
ListViewItem.ListViewSubItem aSubFooItem2 = new ListViewItem.ListViewSubItem(aFooItem, "FooBarBaz/Foo");
aFooItem.SubItems.Add(aSubFooItem1); //Associating these subitems to the parent item
aFooItem.SubItems.Add(aSubFooItem2);
myListView.Items.Add(aFooItem); //Adding the parent item to the listview control
Adding separate styles to the subitems
In the Details mode, if we wanna add subitems that appear different from the parent item (Styling informations), then make the property UseItemStyleForSubItems to true. This property will be under the ListViewItem object. Doing so will allow us to specify seperate style informations for the subitems.
Removing items
Removing items is simple.
using Remove() method
//Remove method takes an object of ListViewItem. So create an object of ListViewItem for the item that needs to be deleted
//and call the method Remove() passing the object that you have created
ListViewItem aFooItem = new ListViewItem("foo");
myListView.Items.Add(aFooItem);
myListView.Items.Remove(foo);
using RemoveAt()
myListView.Items.RemoveAt(0);
Associating images with the list items
This is a nice feature, where we can add images to the ListViewItems (but I am not sure about the number of images that an item can have). For this, we need to have a ImageList with a set of images. Refer how to add images to image list before proceeding. This is done using one of the overloads of the ListView.Items.Add() method that involves ImageIndex. ImageIndex is the index of the image in the ImageList that is associated with the ListView control. ImageList is a list of images. Its like an array of images with an index associated with each images in the image list (called the Imageindex).
myListView.SmallImageList = myImageList; //ImageList where you have the set of images
myListView.Items.Add("foo", 0); //Where the 0 represents the ImageIndex of the image in the imagelist
myListView.Items.Add("bar", 1);
myLIstView.Items.Add("baz", 2);
Adding checkboxes as ListItems
This will enable us to put a CheckBox in each and every row. The programmer can handle the check box’s events through ItemCheck(before state change, this is called) and ItemChecked(after state change, this is called)
myListView.CheckBoxes = true; //To enable the CheckBox mode for the ListView control
myListView.ItemChecked += new ItemCheckedEventHandler(ItemChecked);
These are some of the commonly used features of ListView control.

Please include pictures to make it more understandable. It took me a long time to understand.
Oops. There was some problem imporing the posts from my old blog (pictures were not imported properly). Sorry for the difficulty. Will update very soon.