By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,099 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Listview - get value multiple columns

P: n/a
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
Aug 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"John Devlon" <jo********@hotmail.comwrote in message news:E5********************@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
Aug 28 '06 #2

P: n/a
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
Aug 28 '06 #3

P: n/a
If there are no selected items you will get this error. Check the SelectedItems.Count Property.

See inline.
"John Devlon" <jo********@hotmail.comwrote in message news:xL********************@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
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
Aug 28 '06 #4

P: n/a

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

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


Aug 28 '06 #5

P: n/a
"John Devlon" <jo********@hotmail.comwrote in message
news:09********************@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
Aug 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.