Copy data in a two-dimensional array into a ListView control in C#

The CopyArrayToListView method copies data from a two-dimensional array into a ListView control. First it 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 finishes by looping through the columns, adding the other items for that row as sub-items to the row.

// 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]);
}
}
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.