简体   繁体   中英

Populating listview with List<List<string>>

I'm coding in C# andI have a listview control with few columns, and I'm wondering, how can I add items to it from List<List<string>> . I am able to add items with

var item3 = new ListViewItem(new[] { table[0][0], table[0][1], table[0][2], table[0][3], table[0][5], table[0][7], table[0][8] });

but that doesn't seem right to me, and neither it would work because the amount of Lists is random.

You can use List<T>.ToArray to convert the list into an array which you then pass to the constructor of ListViewItem . As you are only interested in the first subitem of your table, you can just do it like this:

var item3 = new ListViewItem(table[0].ToArray());

You can use the following code to fill all the List<List<string>> into your ListView :

listView1.Items.AddRange(table.Select(list=>new ListViewItem(list.ToArray())))
               .ToArray();

probably you mean like this ?

sample data

item1

List<string> list1 = new List<string>();
list1.Add("item-1.1");
list1.Add("item-1.2");

item2

List<string> list2 = new List<string>();
list2.Add("item-2.1");
list2.Add("item-2.2");

allitem

List<List<string>> listAll = new List<List<string>>();
listAll.Add(list1);
listAll.Add(list2);

string value

string sResult = string.Join(", ", from list in listAll from item in list select item);

list value

List<string> lResult = new List<string>(from list in listAll from item in list select item);

binding

lv.DataSource = lResult;
lv.DataBind();

result

item-1.1 item-1.2 item-2.1 item-2.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM