Logging Experiences

Dealing with ListView control, in the details mode

Posted in C#, Programming, Snippets, Tips and Tricks by Sina Iravanian on May 9, 2011

I needed a piece of code to manage list-view controllers. I needed to use ListView controls in the details mode, which automatically sort its contents when I click on the header, and also pastes the content in a tabular format when I press Ctrl+C or Ctrl+Insert on it. So I created the following classes. It is necessary to note that the code that carries out the sorting stuff when the header is clicked is taken from the following MSDN article:

So these are the contributions of these pieces of code: 1) Sorting ListView items automatically when the header is clicked, 2) Pasting the contents of the ListView control to the clipboard in a tabular format, and 3) Providing an easy to use API to append data to a ListView control.

The following gist contains the code to accomplish this. It consists of 3 files. The ListViewColumnSorter.cs file is responsible for the sorting stuff. The ListViewUtils.cs file provides the utility methods for a ListView control. The main methods that perform the job are located in the ListViewUtils class. The two needed methods are InitListView and AppendToListView. And finally the Usage.cs file provides a small sample demo of the utility methods.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: