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: .

Recommended Answers

All 19 Replies

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

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;

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

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

how to take a field from listview

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 :)

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

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

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.

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.

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:

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..

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.

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

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
commented: Don't bump old threads especially using the wrong language -1

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.

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

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

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.

commented: Hats off to the mod with the +100 wand of thread closure :) +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.