1

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?

2
  • 3
    It's probably more pragmatic to get the values from the underlying data source instead of from the display control. Commented Aug 17, 2012 at 17:44
  • if you are loading object values in listvew then see objectlistview.sourceforge.net/cs/index.html Commented Aug 18, 2012 at 10:00

4 Answers 4

5
var vals = listView1.Items.Cast<ListViewItem>().Select(lvi => lvi.SubItems[1].Text); 
Sign up to request clarification or add additional context in comments.

Comments

2
// 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); 

Comments

1

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

2 Comments

yeah i did like this but the loop is not executed. here is my code for(int i=0;i<=listview1.Items.Count;i++) { listview1.Items[i].Selected = true; string path = sch.m_playlist.SelectedItems[i].SubItems[1].Text; MessageBox.Show(path); }
Are you trying to do this? - for(int i=0;i<=listview1.Items.Count;i++) { string path = listview1.Items[i].SubItems[1].Text; MessageBox.Show(path); }
1
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.