[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Copy ListView data into an array in C#

[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] + "|"); } sb.AppendLine(); } 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.