Hi Guys! Hope you are doing great. Our mission is to give you valuable knowledge related to your field so you keep coming back for what we have to offer. Today, I am going to unlock the details on the C# ListView Control. It displays useful information about list of items by using various views available in the control. You can also have a look at LinkLabel and DateTimePicker that I have discussed previously. I'll try to cover every aspect related to ListView Control so you get a clear idea about this control and you don't need to go anywhere else for finding the information regarding this control. Let's get started.
C# ListView Control
C# ListView Control is used to display list of items by using various views available in the control.
It is very useful in developing the applications where you want the data to be arranged in the form of list.
Creating a ListView Control in Windows Form Application is very easy.
Simply open the windows form application and drag the listview control from the ToolBar to Form.
You can also create it by double clicking the listview control on the ToolBar, it will automatically place the listview control on the Form. Once you create the listview control it will look like the figure below.
You can change its location with the help of mouse.
Once you click on the little arrow available on the top right corner, it will appear like the figure below.
Here you can see the ListView Tasks from where you can add items, columns, groups in the list and can change the view of the list that comes with different view options including largeicon, smallicon, list, title.
LlistView control comes with lots of different properties which you can modify manually.
In order to open the properties window, right click on the listview control on the Form and go to properties windows.
Once you open the properties windows, it will appear like the figure below which comes with different available properties.
Go to properties window and find property "Name" which specifies the unique name of the control which is used to access it in the the main code.
Location and Size Properties
Location property represents the starting point of the listview control on the main Form. In other words, it represents the coordinates of the upper left corner of the control relative to the upper left corner of the container.
Size property defines the size of listview control on the main Form. You can put the width and length on the size property or you can increase the size of the control by clicking the lower right corner of the control and expanding it towards right side.
ForeColor and BackColor Propeties
ForeColor represents the forecolor of the listview control on the main Form.
BackColor property represents the background color of the listview control.
You can change both forecolor and backcolor manually by simply going to the properties window by right clicking on the listview control or you can change them by the help of code. Following lines can be used to change fore and back color in the code.
Items property is very useful in this control that allows you to add items one by one. You can add items manually through this control but better way is using proper code to add items in the listview control.
You can use these lines to add items in the code
ListViewItem lv1 = new ListViewItem("Adnan Aqeel");
lv1.SubItems.Add("123 main street");
First we create three columns naming name, address and age and set the view to details. Then we add above lines in the main code by double clicking the listview control.
CheckBoxes property allows you to add checkbox in the listed item. By default checkboxes property is false, set this property as true in order to add checkboxes of items in the control.
LabelEdit and HoverSelection Properties
LabelEdit is very handy as it allows you to change the text of the items available in the list.
Set this property as true in order to change the text of the items manually.
However, you can also change text of the items dynamically in the code.
HoverSelection property is used to make the item selected when someone hover the mouse on it.
Set this property true in order to make the item selected over hovering the mouse over it.
HotTracking and Alignment Properties
HotTracking property allows you to underline the text of the item and make it prominent when someone hovers mouse over the item in the list. Set this property as true in order to make this property useful.
Alignment property is very useful that allows you to align the items in the list properly. It comes with four options including default, left, top, SnapToGird.
Columns and Groups Properties
Columns property is very important property of the control that allows you to add the columns in the list. This property becomes handy when you are using the Details view of the control.
Groups property allows you to distinguish the classes of items distinctively.
How to Add Columns in the ListView Control
There are two ways to add columns in the listview control. First method is adding the columns manually in the list or other methods is adding columns with the help of code.
In order to add column manually, click on the arrow appearing on the top right corner of the listview control and click on "Edit Columns".
Once you click on the Edit Columns, following figure will appear.
Click on the add button in order to add column in the listview.
In the "text" property you can set the name of your own choice that will appear on the column of listview control.
Or you can add the column using following lines in the code.
From the lines above, you can see this code comes with two arguments in which first arguments represents the column heading and other represents the column width.
Let's create a code for which we add items on the list step by step by the add button, then we remove the selected data from the delete button and then clear all data from the clear button in the windows form application.
When we add one panel, three buttons, three textboxes, three labels and one listview on the main form, it will look like a figure below.
Following is the code to run this application to add items in the list step by step and remove it or clear it completely.
public partial class Form1 : Form
private void btnAddRecord_Click(object sender, EventArgs e)
ListViewItem lv1 = new ListViewItem(txtStudentName.Text);
private void btnDeleteSelected_Click(object sender, EventArgs e)
private void btnClearAll_Click(object sender, EventArgs e)
Following figure shows the code in Visual Studio.
Here's a short video in which I have shown How to use C# ListView Control:
That's all for today. I hope you have enjoyed the article and got to know the features of listview control. We always love when you visit our website to resolve your issues related to engineering. However, if still you feel any doubt or have any question you can ask me in the comment section below. I'd love to help you according to best of my expertise. Stay Tuned!
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow