Copy ListView data into an array in C#


This example uses the following ListView extension method to copy ListView data into a two-dimensional array of strings.

// Return the ListBox's contents in a string[,].
public static string[,] GetListViewData(this ListView lvw)
    // Get the number of rows and columns.
    int num_rows = lvw.Items.Count;
    int num_cols = 0;
    for (int i = 0; i < num_rows; i++)
        if (num_cols < lvw.Items[i].SubItems.Count)
            num_cols = lvw.Items[i].SubItems.Count;

    // Make the array.
    string[,] results = new string[num_rows, num_cols];

    // Populate the array.
    // Note that SubItems includes the items, too.
    for (int r = 0; r < num_rows; r++)
        for (int c = 0; c < num_cols; c++)
            results[r, c] = lvw.Items[r].SubItems[c].Text;

    // Return the result.
    return results;

The method sets num_rows equal to the number of items in the ListView control’s Items collection. It then loops through that collection to see which item has the most sub-items.

Note that an item’s SubItems collection includes the item, too. For example, suppose the ListView control named lvw has a first item that includes the values Apple, Banana, and Cherry. Then lvw.Items[0].Text is Apple and lvw.Items[0].SubItems contains all three values Apple, Banana, and Cherry.

When it loops through the SubItems collection, the program keeps track of the SubItems collection that has the most items. that gives the number of columns we need in the array.

Having found the number of rows and columns it needs, the program allocates the results array. It then loops through the items again, this time copying the ListView data into the array. When it is done, the method returns the result.

Using the extension method is easy. The following code shows how the main program uses the method when you click the Get Data button.

// Get the ListView's contents.
private void btnGetData_Click(object sender, EventArgs e)
    // Get the contents.
    string[,] listview_data = lvwBooks.GetListViewData();

    // Display the contents.
    StringBuilder sb = new StringBuilder();
    int num_rows = listview_data.GetUpperBound(0) + 1;
    int num_cols = listview_data.GetUpperBound(1) + 1;
    for (int r = 0; r < num_rows; r++)
        for (int c = 0; c < num_cols; c++)
            sb.Append(listview_data[r, c] + "|");
    txtResult.Text = sb.ToString();

This event handler uses the extension method to get the ListView control’s values. It then loops through them adding the values to a StringBuilder. When it is finished, the program displays the resulting string in a text box.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in arrays, controls, strings and tagged , , , , , , , , , , . Bookmark the permalink.

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.