Introduction
ListView is a more powerful and flexible version of Listbox. While ListBox enables you to display only one list of items, ListView allows you to accommodate a collection of miscellaneous items (including Checkbox). For example, a list of files with information such as Name, Size, Type, Modified date, similar to that of Windows explorer. Similar to the ListBox, the ListView allows you to select one or multiple items at one time and do operations based on the selection.
Populating a ListView from different data sources
There are a few articles and tutorials on the ListView control on the Internet, including a very comprehensive and technical references on MSDN. There are also questions about the databinding capacity of a ListView. Well, in Windows forms, there are databinding controls, the most powerful one being the DataGridView control.However, the ListView control is not one of them. To bind it with data, be it XML file or database records, your computer's file system or a HashTable, you must to do it yourself.
I am sure you know the data-fetching drill. For an XML file, you either use a fully featured XMLDocument or a forward-read only XMLReader to read in the data and populate the ListView control; for a database (Access, SQL etc.), you employ an all-inclusive DataSet or a light-weight DataReader; for file and directories, you enlist DirectoryInfo to get all the files and subdirectories; and for Hashtable, an IDictionaryEnumerator that enables you to loop through all the items and fill them in the ListView.
A ListView with four views
One of my favorite features of the ListView is that it has four views, similar to the views that appear in Windows explorer:
LargeIcon
SmallIcon
Details
List
The LargeIcon/SmallIcon view displays items in rows of large/small icons, Details view arrange items in a grid with headers, and List view show them in a vertical list of small icons. A ListView has another view Tile, however it is not often used.
It is a breeze for a ListView to switch from view to view. For LargeIcon or SmallIcon view to become truly views of large/small icons, you have to assign the LargeImageList/SmallImageList of a ListView to an ImageList of one or more images, by using the following code:
Code Listing 1
The following example implements a ContextMenuStrip to demonstrate the different views of a ListView.
ContextMenuStrip is yet another beautiful Windows Form control, an updated version of ContextMenu. You may still use ContextMenu if you feel more comfortable with it. To add a ContextMenuStrip, drag one from your toolbox and drop to the form. You may add the menu items at design time by directly typing it out in the menu, or you may add them at run time, as in the following code example:
Now implement a simple contextMenuStrip1_ItemClicked function to handle the user's menuitem-click event. To change the views, simple set the View property of the ListView to the one that the user dictates:
Code Listing 3
Now run the application, right click on the ListView to switch the views.
The Details View of the ListView Control
The LargeIcon View of the ListView Control
Sorting a ListView by any Column
The ListView control has a built-in Sort method and you can sort in either ascending or descending order by setting the property Sorting to SortOrder.Ascending or SortOrder.Descending respectively. However, the Sort method only sort items in the first column by default and it only sorts items as Strings. To sort columns other than the first one or to order items other than string objects, you have to create a class that implements the IComparer interface and set the ListViewItemSorter property to an instance of that class.
The following example creates a class called ListViewItemComparer that implements the IComparer interface. It provides mechanism for Numeric, DateTime and String comparison. You may add other comparison logic as needed. The class has a contructor that takes no parameter, however it has three properties for you to specify the column to be sorted, the column's datatype and sorting order.
Code Listing 4
Now you have the class to sort, you need to set it in motion. To do so, you create an event-handling method for the ColumnClick event of the ListView. In this method, you create an instance of your custom sorter, ListViewItemComparer, you set the ListViewItemSorter to it, specify the different properties as you desire, and finally, you call the Sort() method of the ListView.
Code Listing 5
Summary
In this article, you have seen how to use the ListView control, how to switch views and how to apply additional sorting features. The code provided in this article was written in C#, if you need it in VB.NET, please feel free to email me and I will happy to provide that. Any other comments or suggestions are very welcome.
References
Working with ListView
ListView Control - Introduction
ListView Class (System.Windows.Forms)
About Xun Ding
 |
Web developer, Data Analyst, GIS Programmer
This author has published 12 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Choose your preferred data layout with RadListView for ASP.NET AJAX
read more
Using ASP.NET 3.5's ListView and DataPager Controls: Inserting Data
read more
RadDock for Winforms Q2 2009 Beta has landed!
read more
ASP.NET MVC 2.0 and VS 2010 plan now public
read more
ASP.NET MVC and the templated partial view (death to ASCX)
read more
CodeDigest.Com Article,Codes,FAQs - April,2009
read more
Easy way to create a web-based AJAX SFTP Client application
read more
WPF Release History : Q1 2009 SP1 (version 2009.1.413)
read more
Twilight 1.5: Multiple Views with MVVM
read more
ASP.NET Data Control Events
read more
|
|
Please login to rate or to leave a comment.