Easily add column headers and items to a ListView control in C#


Normally to add a row to a ListView control, you first add a new item and then add sub-items to the item. The following code shows an AddRow extension method for the ListView class that makes adding new rows easier.

// Add the items as a new row in the ListView control.
public static void AddRow(this ListView lvw, string[] items)
    // Make the main item.
    ListViewItem new_item = lvw.Items.Add(items[0]);

    // Make the sub-items.
    for (int i = 1; i < items.Length; i++)

This code takes a parameter array of strings to be added to the row. It uses the first value to make a new item. It then adds the other values as sub-items.

If lvwBooks is a ListView control, you can use this method as in the following:

lvwBooks.AddRow(new string[] {
    "C# 5.0 Programmer's Reference",
    "978-1-118-84728-2", "960", "2014" });

Because the list of items is a parameter array, you can also use the extension method as in the following:

    "Beginning Database Design Solutions",
    "978-0-470-38549-4", "552", "2008");

To add column headers to a ListView control, you normally add new items to its Columns collection. The following
MakeColumnHeaders extension method makes it easy to add headers all at once, specifying their titles and alignments.

// Make the ListView's column headers.
// The ParamArray entries should alternate between
// strings and HorizontalAlignment values.
public static void MakeColumnHeaders(this ListView lvw,
    params object[] header_info)
    if (header_info.Length % 2 != 0)
        throw new ArgumentException(
            "The method must have an even number " +
            "of header_info parameters");

    // Remove any existing headers.

    // Make the column headers.
    for (int i = 0; i < header_info.Length; i += 2)
            (HorizontalAlignment)header_info[i + 1]);

The method first verifies that the header_info parameter array contains an even number of items. It then clears the ListView control’s Columns collection. Finally, it adds the header values specifying a column width of -1 (size to fit the data) and the horizontal alignment passed in through the parameter array.

The main program uses the following code to call this extension method.

// Make the ListView column headers.
    "Title", HorizontalAlignment.Left,
    "URL", HorizontalAlignment.Left,
    "ISBN", HorizontalAlignment.Left,
    "Pages", HorizontalAlignment.Right,
    "Year", HorizontalAlignment.Right

The program does two other interesting things with its ListView control. First, it uses the following extension method to set all of the control’s column sizes at once.

// Set all columns' sizes.
public static void SizeColumns(this ListView lvw, int size)
    for (int i = 0; i < lvw.Columns.Count; i++)
        lvw.Columns[i].Width = -2;

The main program uses the following statement to call this method.


Finally, the main program uses the following code to size the form to fit the ListView control.

// Make the form big enough to show the ListView.
Rectangle item_rect =
    lvwBooks.GetItemRect(lvwBooks.Items.Count - 1);
this.ClientSize = new Size(
    item_rect.Left + item_rect.Width + 25,
    item_rect.Top + item_rect.Height + 25);

The code uses the ListView control’s GetItemRect method to get the bounding rectangle for the last item in the control. It then uses that rectangle’s dimensions to make the form big enough to display the whole ListView at once.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in controls, user interface and tagged , , , , , , , , , . Bookmark the permalink.

1 Response to Easily add column headers and items to a ListView control in C#

  1. Pingback: Sort ListView controls by all columns or clicked columns in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.