MorkaLork Development

Interesting stuff I've picked up over the years...

ListView

2009-05-16 16:11:02 | 728 views | listview use listview checkbox

The Basics



ListView came into my viewport when I, as an old VB/vba programmer, tried to create a multicolumn listbox. This couldn't be done for some reason in C# and I went temporarily insane. Then I found the ListView.
The listview works as sort of a high-tech multicolumn listbox. You can do alot with it that you can't do with a listbox (by default), such as using checkboxes and images with ease.
The main reason to use a listview is to display data and let the user interact with it. Let's go through how to use this control.

Getting into it



A simple listview example
This is what we'll make...

First, you will have to create headers for your listview. This might be a good time to think about what you want to put into it. In this example, we want to add information about people, a common need in todays business, and we want to display Name, Age and level of incompetence.

First, we need to set the display level of the listview to detail, otherwise, our column headers won't show:


listView1.View = View.Details;


After doing that we will set the properties for checkboxes, making them available:


listView1.CheckBoxes = true;


We will also set the FullRowSelect to true, giving the user the possibility to select entire rows complete with subitems, and Gridlines to false, not showing the gridlines:


listView1.FullRowSelect = true;
listView1.GridLines = false;


Now we've set the properties of the listview that we need to set (we don't need to, but it's a good thing to do). Now we'll create column headers, something that is a bit tricky in C#.
Problem is that listview headers tend to have a life of their own. MSDN claims that a column header width should be set to -2 so that it will autosize, but that's no good. What happens is that your column headers dissapear far beyond the form (if it's to small) and the user will have to scroll. So what we'll do it set a width for the listview and use that value when setting the column headers:


listView1.Width = 260;

listView1.Columns.Add("Name", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Age", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Idiot", 60, HorizontalAlignment.Left);


The adding itself is quite self-explanatory, but the synthax on this particular overload looks like this:
listview.Columns.Add(string text, int width, HorizontalAlignment textAlign)

We've set the width to static values making it fit perfectly to our form. This is hardly effective or neat, but it works. For an article that will show you how to actually overcome this problem, please go here: http://www.codeproject.com/KB/list/myListViewNoSize.aspx.

Now to the loading of information. Normally you might get your data from a SQL database, and XML file or just plain textfiles, I won't go through all that job since this is an example, so I'll just create some fake data and enter it.

When entering data, you create a ListViewItem object that you will later add to the ListView. This differs from the way you might be used to working with listboxes, but it's quite usefull. The ListViewItem has it's own properties and method to add data and set for example checkboxes, so it makes it easy to have absolute control of what you're adding:


ListViewItem item1 = new ListViewItem("Rod");
item1.Checked = false;
item1.SubItems.Add("8");
item1.SubItems.Add("Yes");


First we create an item. The base item will be what's showing in the first row. After that, we set the Checked property for this particular item to false(we don't care about the idiots, our firm is looking for smart people!). We add two subitems, age and level of incompetence.
We create two more items, item2 and item3, and then we add them to the listview:


listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });


We can choose to just use .Add or .AddRange, but since we add alot of items and once, we use .AddRange and enter the items as an array.

That's the basics of the listview control, good luck:


The Full Code




using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace listview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//==============SETUP================================================
//So that we can see the details :)
listView1.View = View.Details;
//Implement checkboxes
listView1.CheckBoxes = true;
//So that the entire item (and subitem) is selected by the user
listView1.FullRowSelect = true;
//Hide gridlines
listView1.GridLines = false;

//Since we don't want to have a listview with a scrollbar, we set
//the width at startup and apply this to the columns
listView1.Width = 260;

//100 + 100 + 60 = 260, the total width of the listview
listView1.Columns.Add("Name", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Age", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Idiot", 60, HorizontalAlignment.Left);

//===============LOADING=============================================
ListViewItem item1 = new ListViewItem("Rod");
item1.Checked = false;
item1.SubItems.Add("8");
item1.SubItems.Add("Yes");

ListViewItem item2 = new ListViewItem("Todd");
item2.Checked = false;
item2.SubItems.Add("10");
item2.SubItems.Add("Yes");

ListViewItem item3 = new ListViewItem("Lisa");
item3.Checked = true;
item3.SubItems.Add("10");
item3.SubItems.Add("No");

listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });

}
}
}



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.