Listview - get value multiple columns

J

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
 
A

Al Reid

John Devlon said:
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
 
J

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
 
A

Al Reid

If there are no selected items you will get this error. Check the SelectedItems.Count Property.

See inline.


John Devlon said:
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
 
J

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

Claes Bergefall

John Devlon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top