[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 data in a two-dimensional array into a ListView control in C#

ListView control

The CopyArrayToListView method shown in the following code copies data from a two-dimensional array into a ListView control.

// Copy a two-dimensional array of data into a ListView. private void CopyArrayToListView(ListView lvw, string[,] data) { int max_row = data.GetUpperBound(0); int max_col = data.GetUpperBound(1); for (int row = 0; row <= max_row; row++) { ListViewItem new_item = lvw.Items.Add(data[row, 0]); for (int col = 1; col <= max_col; col++) { new_item.SubItems.Add(data[row, col]); } } }

The code first uses the array's GetUpperBound method to get the upper bounds of the first and second dimensions (rows and columns respectively).

Next the code loops through the rows. For each row, it adds a new item to the ListView. It then loops through the columns, adding the other items for that row as sub-items to the row.

Download the example to experiment with it and to see additional details.

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