1.11M Members

Listview: How do I get the data from a column in a selected row

 
1
 

I wanna get the data in the clicked row in column 4. I've tried with

lv.selecteditems.tostring();

but that's wrong. Can someone please help me :rolleyes: .

 
0
 

Hi,

Try lv.selecteditems[0].subitems[2].text;
If you use ItemActivated or SelectedIndexChanged event then you can avoid lv.selecteditems[0] and directly call the ListViewItem passed as a parameter.

Loren Soth

 
0
 

Did you mean like this (it works) or can you do it in some other way?

ListView lv = (ListView)sender;
tbSearch.Text = lv.FocusedItem.SubItems[0].Text;

 
0
 

Hi,

I thought one of those events pass the ListViewItem object as parameter just like TreeView returns TreeViewNode but I was mistaken.
You can use just "tbSearch.Text = lv.FocusedItem.SubItems[0].Text;" and it would do it.

Loren Soth

 
0
 

ah okej the important thing is that it works :cheesy: thanks for the idea

 
0
 

how to take a field from listview

 
0
 

You must simply "walk through the listview" till you get the index of selected row and then you can do whatever you want in that row LIKE THIS:

for (int i = 0; i < lv.Items.Count; i++)
                 // is i the index of the row I selected?
            if (lv.Items[i].Selected == true)
                {  
//I show here the second field text (SubItems[1].Text) from the selected row(Items[i]) 
                    Message.Show(lv.Items[i].SubItems[1].Text);
                    break;
                }

It is simple and clear :)

 
0
 

please mark this thread as SOLVED if it's helpful. AND IT SHOULD BE.

 
0
 

You shouldnt need to walk through the contents, far from it. However, if it is solved, please mark it as such

 
0
 

however that's the truth you walk through them, like i said till oyu get the selected one. It's not like a menu, you'll never find a SelectedItem property on a listview.

 
0
 

No but thats what the selecteditems list is for, or the selected indicies..

so you can do

foreach (int i in listView1.SelectedIndices)
            {
                MessageBox.Show(listView1.Items[i].Text);
            }

much better than checking each one if its selected.

 
0
 

Thanks LizR, i agree, however I answered first:cool: . So what's the rule? Who gets +1 to 'solved threads'. Who answered first (with a good answer) or who answered last? :twisted:

 
0
 

Thats upto the guy who marks it as answered, wether he goes for the first answer, or the better answer, or in fact the wrong answer.. its entirely his choice. In a way Lord Soth beat you too it too, but, its upto the guy who grants the point..

 
0
 

I'd agree with LizR on this. The selected indices will be smaller than the entire list. If a list has a few hundred items and the user has selected 3, the loop becomes considerably less expensive on the indices rather than the entire list.

 
0
 

Theres SelectedItems which would do the items, or selectedIndicies, either depending on what you want would be a far more efficient method.

 
-1
 

Insted of going through all the items go through only the selected items

Dim col As ListView.SelectedListViewItemCollection = lviFolders.SelectedItems
        Dim itm As ListViewItem

        For Each itm In col
             Debug.WriteLine(itm.SubItems(0).Text)
           Debug.WriteLine(itm.SubItems(1).Text)
        Next
 
0
 

I knew that from a couple of years lol.... cause..... it's been a while since 2008 don't you think? :)) and in a C# forum it's recomended to use C# language, not VB :)).

Cheers.

 
0
 

sorry about the C# confusion, i've posted it for someone new to VS

 
0
 

Has no1 looked at the fact that this thread was started in 06?

 
2
 

We appreciate your help. Please do not resurrect threads that are years old. By doing so you run the risk of confusing current posters and/or reopening old wounds.

Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Closed.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article