Shankar G
Shankar G

Reputation: 39

How to get the values in all rows of a particular ListView column?

I have a ListView with three columns and I have added 4 records in the ListView. I would like to get the value of the 2nd column value of each record. How to implement it?

Upvotes: 1

Views: 3608

Answers (4)

Shankar G
Shankar G

Reputation: 39

foreach(ListViewItem itm in listView1.Items){ itm.Text/*first column of the particular item*/   itm.SubItem[1].Text/*second column of the particular item*/}

since foreach loop can access more fast than for loop

Upvotes: 1

thersch
thersch

Reputation: 1396

// Convert items to an IEnumerable for LINQ usage
ListViewItem[] items = new ListViewItem[4];
listView.Items.CopyTo(items, 0);

// Use LINQ to get values
IEnumerable<string> secondColumnValues = items.Select(_ => _.SubItems[1].Text);

Upvotes: 2

aquinas
aquinas

Reputation: 23786

var vals = listView1.Items.Cast<ListViewItem>().Select(lvi => lvi.SubItems[1].Text);

Upvotes: 5

ygssoni
ygssoni

Reputation: 7349

loop through all the rows and try this.

Subitems[columnnumber] will be the column numbr of the required field

lv.Items[i].SubItems[1].Text

Upvotes: 1

Related Questions