Click here to Skip to main content
12,884,509 members (32,602 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 19 May 2004

Save ListView settings

, 19 May 2004
Rate this:
Please Sign up or sign in to vote.
How to save ListView settings.

Sample Image - ListviewSettings.jpg


Most of the time, it is important to persist the width of columns in a list view. And this can be done easily. But sometimes you may need to persist, the order, whether a column is a required column, and whether the column should be displayed or not.

The Question

The order and the width are already provided, but whether a column is required or should be displayed is the reason of this article, and my question: "How do I persist these values?".

The Answer

The ListView in itself does not know anything about this information. So, I decided to implement a ListView Settings dialog. Columns information is stored to the registry in the order that they should be displayed.

Steps that require attention in the ListView Settings dialog are:

  1. Add columns to the dialog.
    private void AddDonorCols()
        foreach( MyColumns col in reg.DonorCols)
            ListViewItem it =  listView1.Items.Add(;
            it.Checked =;
        listView1.Items[0].Selected = true;

    Read columns information from the registry and initialize list with information.

  2. Moving items up or down.
    private void SwapElements(int idx)
        ListViewItem it1 = listView1.Items[idx];
        ListViewItem it2 = listView1.Items[idx -1];
        listView1.Items.RemoveAt(idx - 1);
        SwapCols(ref reg.m_DonorCols[idx], ref reg.m_DonorCols[idx - 1]);
        // Needed to Update Width value properly.
        OnSelectedIndexChanged(null, null);

    This function is used by the Move up and Move Down buttons. When moved down, the index (idx) of the element being moved is the index of the next element in the list. When moved up, the index (idx) of the element being moved is passed to the function.

  3. Check box is clicked.
    private void OnItemChecked(object sender,
            System.Windows.Forms.ItemCheckEventArgs e)
        int idx = e.Index;
        if ( reg.m_DonorCols[idx].required == true)
            // Fields that are required should remain checked
            e.NewValue = CheckState.Checked;
        // Q: Why don't I worry about updating my (.show) state?
        // A: Because I enable buttons base on the ListviewItem state
        //    and not the state of (.show) state.
        //    The (.show) state will be updated once when OK is clicked.

    When an item is required, you cannot change its checked state. So, you set the NewValue to checked.

Back to the main ListView

I started by changing the ListView property HeaderStyle to NonClickable, so I will not have to worry about dragging columns. The only worry I will have will be resizing columns, which has been taken care by the article: Persist ListView settings with serialization by dfontanesi.

Steps that need notice in the main ListView:

  1. Initialize the ListView width:
    private void InitDonors()
        MyReg reg = new MyReg();
        //-- Add Column Header
        foreach( MyColumns col in reg.m_DonorCols)
            if (
                    col.width, HorizontalAlignment.Left);
  2. Save ListView width:
    private void SaveListViewWidths()
        MyReg reg = new MyReg();
        foreach( ColumnHeader column in listView1.Columns )
            LV_COLUMN pcol = new LV_COLUMN();
            pcol.mask = LVCF_ORDER;
            bool ret = SendMessage(listView1.Handle,
                LVM_GETCOLUMN, column.Index, ref pcol);
            reg.m_DonorCols[column.Index].width = column.Width;

Well peace, I am out.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Guy Baseke
Software Developer (Senior)
Canada Canada
Software engineer

You may also be interested in...


Comments and Discussions

GeneralListview Savesettings Pin
Jorge Estrada20-Dec-05 19:37
memberJorge Estrada20-Dec-05 19:37 
GeneralRe: Listview Savesettings Pin
Guy Baseke21-Dec-05 12:16
memberGuy Baseke21-Dec-05 12:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 20 May 2004
Article Copyright 2004 by Guy Baseke
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid