Search code examples
c#arrayslistviewlistviewitem

ListView population from array


I'm attempting to fill a ListView with items from a 2D array. The array would be something like this:

data:

john,doe,jd@test.com,555-123-4567,test
jane,doe,janed@test.com,555-123-4568,test2
test,testing,tt@test.com,555-123-4569,test3

etc.

The code I'm using is this:

for (int row = 0; row < data.Length; row++)
{
   var item = new ListViewItem();
   item.Text = data[row][0];

   for (int col = 1; col < data[row].Length; col++)
   {
      item.SubItems.Add(data[row][col]);
   }
   ContactsList.Items.Add(item);
}

However, when I run the code it ends up pulling just the first column and adding it to the same ListViewItem and displaying just that single row. So for the data above it would give me:

john jane test

and nothing else.

Thank you for any help you can provide.

EDIT 1: here is the ContactList code

this.ContactsList = new System.Windows.Forms.ListView();
...
this.ContactsList.AccessibleDescription = "Contacts List";
this.ContactsList.AccessibleName = "ContactsList";
this.ContactsList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
   this.FirstName,
   this.LastName,
   this.Email,
   this.PhoneNumber,
   this.Note});
this.ContactsList.Location = new System.Drawing.Point(12, 38);
this.ContactsList.Name = "ContactsList";
this.ContactsList.Size = new System.Drawing.Size(976, 574);
this.ContactsList.TabIndex = 8;
this.ContactsList.UseCompatibleStateImageBehavior = false;
this.ContactsList.SelectedIndexChanged +=
   new System.EventHandler(this.ContactsList_SelectedIndexChanged);

This code was generated by the designer, and being fairly junior in C# I'm not sure if I missed something.


Solution

  • So after messing around with it for a bit I found the answer!

    The view for the listview needed to be set to detailed.

    Sorry for the newbie question, hope this helps someone else.