Click here to Skip to main content
Licence CPOL
First Posted 30 Jul 2007
Views 22,275
Downloads 700
Bookmarked 37 times

ListView Personalization

By | 30 Jul 2007 | Article
An article on the personalization and persistance of ListView settings
Screenshot - Screen.jpg

Introduction

I had the need to personalize a ListView frequently. As always, The Code Project was my first port of call. Like most good/lazy developers, I'd rather reuse than write from scratch unless I really have to. I found many articles which came close but never quite hit the mark. So I took Guy Baseke's article: Save ListView Settings and modified it to what I needed. I've also tried to include some best practices in the small library I've produced so that people who are not familiar with some of the community tools have a working example that they can refer to.

Background

My biggest problem with the original sample is that it saved the settings to the registry. While this may be common practice, it does require write privileges for the registry which are frequently not available to all users. So it provided me with the perfect opportunity to read up on isolated storage which does not require elevated privileges.

Using the Code

Using the library is trivial. Simply populate your ListView in the most appropriate manner. You'll probably need an instance level ListViewPersonalisationManager variable to track the changes for before and after your personalization.

InitializeComponent();

//
// Your ListView should be instantiated and populated, 
// create a unique guid for each ListView you wish to personalize. 
// The guid is used to differentiate each ListView you wish to personalize.
//            

Guid id = new Guid("{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}");
manager = new ListViewPersonalisationManager(listView1, id);
// If you have previous settings persisted, they will automatically be 
// applied to the ListView by the ListViewPersonalisationManager

Before displaying the personalization dialog, it is important to synchronize the ListView with the manager in case any columns have been moved or resized.

private void OnSettings(object sender, System.EventArgs e)
{
    // Synchronize any changes made to ListView
    manager.SyncWithListView();

    ColumnSettingsUI dialog = new ColumnSettingsUI(manager.ColumnDetails);
    if ( dialog.ShowDialog() == DialogResult.OK)
        manager.UpdateListView(dialog.ColumnDetails);
}

To persist the settings to disk:

// True to synchronize to the current state of the ListView or  
// false to use current manager state.
manager.SaveSettings(true);

Points of Interest

Technology areas covered:

  • Isolated storage
  • Serialization
  • NUnit
  • NCover
  • NAnt
  • NDoc
  • FxCop

History

  • 30th July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mark Ritchie

Other
n.runs
Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralYou smell Pinmemberjohn allan12342:42 10 Jul '09  
GeneralUserScopedSettings And ApplicationScopedSettingAttribute PinmemberMichael Sync0:03 31 Jul '07  
AnswerRe: UserScopedSettings And ApplicationScopedSettingAttribute PinmemberMark Ritchie12:09 5 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 30 Jul 2007
Article Copyright 2007 by Mark Ritchie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid