Adding Multiple Arrays To a ListView Control

TeachMe

Member
Joined
Jun 27, 2012
Messages
16
Programming Experience
Beginner
I have 5 Different arrays that I'm trying to add to 5 different columns in a ListView, BUT I am only going to use 2 Arrays & 2 Columns to make it less complicated for this example.

So I'm trying to add 2 Arrays that have "Product Titles" & "Prices" that I'm trying to put into 2 columns of a Listview which looks like the image that I have attached below:

MultipleArraysIntoListview.jpg

I have tried numerous times to use the "For each" & "For loop" methods for adding both arrays to the listview, but with NO success. I'm really not sure how to do it. I will have a button1 that the user can click & it will add the arrays to the list. The products were collected from my site & added into 2 separate Array Lists that I would like to add to the listview1 via button1 click. Would appreciate the help. Thanks in advance.
 
Firstly, why do you have five arrays in the first place? You must have populated them at some point and creating multiple arrays like that is a poor choice. If it's within your control, change that. Define a type that has five properties and create instances of that type, populating the five properties with the five values that you would otherwise put in the separate arrays, then create a simple array of those objects or perhaps even a collection.

As for your specific question, a For loop is the correct option. On each iteration, you use the loop counter as an index into all five arrays, allowing you to get all five values and add them to the ListView. The3 mistake that many people make is to add one item for each value. Each item represents a row in the ListView so you need to add one item containing all five values. You use the first value to create the item and then add four subitems. The MSDN documentation for the ListView class has a code example that shows how to add items with subitems so you should be reading that and, to be frank, should have already. VS has a Help menu for a rerason.
 
Back
Top