Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data from a List<List<String>> to a list view

i'm having a List<List<String>> MyList and which contains

    { "A1","B1","C1" }
    { "A2","B2","C2" }
    { "A3","B3","C3" }

i need to add this to a ListView control with three columns

so the list view will be like

__Clm1________________Clm2________________Clm3_______________

   A1                  A2                 A2
   B1                  B2                 B3
   C1                  C2                 C3

Is there any way to do this using LINQ

EDIT Here is my solution for this :

ListViewItem[] lItem = MyList.Select(
                                          X => new ListViewItem(X.ToArray())
                                    ).ToArray();
listView1.Items.AddRange(lItem);

But here i need to add an Index.... The List view will be like

___S.No __Clm1________________Clm2________________Clm3_______________
   1      A1                  A2                 A2
   2      B1                  B2                 B3
   3      C1                  C2                 C3

How to add index to the listview?

like image 265
Thorin Oakenshield Avatar asked Nov 03 '10 06:11

Thorin Oakenshield


1 Answers

Personally I find this a lot cleaner:

var myList = new List<List<string>>()
{
    new List<string>(){ "A1","B1","C1" },
    new List<string>(){ "A2","B2","C2" },
    new List<string>(){ "A3","B3","C3" },
};

listView.BeginUpdate();    
foreach (var row in myList)
{
    var item = new ListViewItem(listView.Items.Count.ToString());
    item.SubItems.AddRange(row);
    listView.Items.Add(item);
}
listView.EndUpdate();
like image 163
jgauffin Avatar answered Sep 30 '22 21:09

jgauffin