Home All Groups Group Topic Archive Search About

Listview - get value multiple columns

Author
28 Aug 2006 12:13 PM
John Devlon
Hi,

Does anyone know how to get a value of a second column  of a selected item
in Listview.

I've create a listview and added this code

Listview.Items.Clear()
Listview.Columns.Clear()
Listview.View = View.Details

Listview.Columns.Add("ID", 0, HorizontalAlignment.Left)
Listview.Columns.Add("Toppic", 150, HorizontalAlignment.Left)
Listview.Columns.Add("Name", 150, HorizontalAlignment.Left)
Listview.FullRowSelect = True

I can retreive the data from the first column, i've selected...

myString = Listview.SelectedItems.Item(0)

... but I can't get any data out of the next columns i've selected...

myString2 = Listview.SelectedItems.Item(1)
myString3 = Listview.SelectedItems.Item(2)

.... this doesn' work ...

Does anyone have any idea's

Thanx

John

Author
28 Aug 2006 12:18 PM
Al Reid
Show quote Hide quote
"John Devlon" <johndev***@hotmail.com> wrote in message news:E5BIg.45995$Dj3.318499@phobos.telenet-ops.be...
> Hi,
>
> Does anyone know how to get a value of a second column  of a selected item
> in Listview.
>
> I've create a listview and added this code
>
> Listview.Items.Clear()
> Listview.Columns.Clear()
> Listview.View = View.Details
>
> Listview.Columns.Add("ID", 0, HorizontalAlignment.Left)
> Listview.Columns.Add("Toppic", 150, HorizontalAlignment.Left)
> Listview.Columns.Add("Name", 150, HorizontalAlignment.Left)
> Listview.FullRowSelect = True
>
> I can retreive the data from the first column, i've selected...
>
> myString = Listview.SelectedItems.Item(0)
>
> .. but I can't get any data out of the next columns i've selected...
>
> myString2 = Listview.SelectedItems.Item(1)
> myString3 = Listview.SelectedItems.Item(2)
>
> ... this doesn' work ...
>
> Does anyone have any idea's
>
> Thanx
>
> John
>
>

Look at the SibItems collection.

myString2 = Listview.SelectedItems.Item(0).SubItems(1).Text

--
Al Reid
Author
28 Aug 2006 12:58 PM
John Devlon
Many thanks for your help ...

At first it worked great

When using it like this ..

Private Sub Listview_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Listview.SelectedIndexChanged
    txtName.Text = Listview.SelectedItems.Item(0).SubItems(1).Text
End Sub

Strange problems occure ...

The first time I select something, it works fine.. the data from the
listview go's into the textfield.
When i select something else, I get a ArgumentOutOfRangeException ...

Any idea's?

john
Author
28 Aug 2006 1:08 PM
Al Reid
If there are no selected items you will get this error.  Check the SelectedItems.Count Property.

See inline.


"John Devlon" <johndev***@hotmail.com> wrote in message news:xLBIg.46054$Oy3.583802@phobos.telenet-ops.be...
> Many thanks for your help ...
>
> At first it worked great
>
> When using it like this ..
>
> Private Sub Listview_SelectedIndexChanged(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles Listview.SelectedIndexChanged
   If Listview.SelectedItems.Coun > 0 Then
>     txtName.Text = Listview.SelectedItems.Item(0).SubItems(1).Text
   Endif
Show quoteHide quote
> End Sub
>
> Strange problems occure ...
>
> The first time I select something, it works fine.. the data from the
> listview go's into the textfield.
> When i select something else, I get a ArgumentOutOfRangeException ...
>
> Any idea's?
>
> john
>
>

--
Al Reid
Author
28 Aug 2006 2:33 PM
John Devlon
Hi,

I've tested the application using your code and I found the problem ...

When selecting something else in a Listview, the methode that handles the
SelectedIndexChanged
is triggerd twice....

First, it sets the selectedItems to 0, then to 1, represting the new
selected value ...

Strang, but the problem is solved ..

Many, Many thanx....




Show quoteHide quote
> If there are no selected items you will get this error.  Check the
> SelectedItems.Count Property.
Author
28 Aug 2006 3:12 PM
Claes Bergefall
Show quote Hide quote
"John Devlon" <johndev***@hotmail.com> wrote in message
news:09DIg.46163$dQ5.673913@phobos.telenet-ops.be...
>
> Hi,
>
> I've tested the application using your code and I found the problem ...
>
> When selecting something else in a Listview, the methode that handles the
> SelectedIndexChanged
> is triggerd twice....
>
> First, it sets the selectedItems to 0, then to 1, represting the new
> selected value ...
>
> Strang, but the problem is solved ..

Not that strange. This is by design. When you select an item in a ListView
it first deselects the previously selected item and this triggers the first
SelectedIndexChanged (were count = 0). Then it selects the item you clicked
on and this triggers the second SelectedIndexChanged (were count = 1). Hence
the check on count is necessary.


   /claes