C# Version.

This tutorial will show how we can use the ListView control to display a list of items on a Windows Form, under different headers. The ListView control is very flexible, and is similar to the
Windows File Explorer. We are able to use the ListView to output information to the user under several headings.

In this example, we will add a ListView to our form and use buttons to add and then manipulate the data we add – removing and clearing. We will also show how we can change the view of the ListView.

To start, we add a ListView control, four buttons, and a group of 5 Radio Buttons. Our Windows Form Designer generated code looks like this:

This is the code that is generated by Visual Studio when we design our form. Yours may look different.

Once we have added all of our controls to the form, we want to click on the Smart Tag of the ListView and choose Edit Columns. For this example, we are going to use Name, Age, and City.

Once we have done this, we can start with the code-behind. Although the first thing we want to code is our Add button. Choose one of the buttons and set the text to Add, then double-click it to create the event handler. Before we write any code, we first need to add a reference to our project. We are wanting to make use of a popup box that will ask the user for input, just like VB’s InputBox. Unfortunately C# does not have an equivalent, but we can reference the Visual Basic DLL and use that one. Right-click the References folder in the Solution Explorer, and choose
Microsoft.VisualBasic from the .NET tab. Then we can use the following code:

This code is for the Add button. We are simply adding to the ListView columns – Name is the main element, and Age and City are the SubItems of that element. For each item, we use the InputBox to gather information from the user.

Next, we can add code to the other buttons. One will be used to remove all items from the ListView, another to remove the selected item(s) from the ListView, and the third button will be the Exit button:

Note that these are very simple methods, but can be powerful tools.
Finally, we want to add code to our RadioButtons. We will use them to change the view of the ListView. The ListView control has 5 built-in views, which we will use to switch between:

Download Source Files