Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net retrieve items from Listview on SelectedIndexChanged

Posted on 2013-11-16
5
Medium Priority
?
1,622 Views
Last Modified: 2013-11-16
I am trying to retrieve items from a Listview SelectedIndexChange event and am struggling to find a way to do it.

I have included my code that populates the Listview and that is working ok. All I need to do is put the results in a string 'str' which I declared elsewhere.

I would be grateful if someone could show me a way or point me in the right direction.



Sub filllistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from Customers", cn)
            dr = cmd.ExecuteReader()
            While dr.Read()
                ListView1.Items.Add(dr(0).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))

            End While

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

Open in new window

0
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Alok-Agarwal
ID: 39653076
Please refer a sample code below from some other example.

' Uses the SelectedItems property to retrieve and tally the price  
' of the selected menu items.
Private Sub ListView1_SelectedIndexChanged
    (ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles ListView1.SelectedIndexChanged

    Dim breakfast As ListView.SelectedListViewItemCollection = _
        Me.ListView1.SelectedItems
    Dim item As ListViewItem
    Dim price As Double = 0.0
    For Each item In breakfast
        price += Double.Parse(item.SubItems(1).Text)
    Next

    ' Output the price to TextBox1.
    TextBox1.Text = CType(price, String)
End Sub
0
 
LVL 5

Accepted Solution

by:
Alok-Agarwal earned 2000 total points
ID: 39653078
so basically you will have to declare a variable of ListView.SelectedListViewItemCollection  Type and then loop through each of those using for each loop. To retrieve the selected item text you can use (item.SubItems(1).Text
0
 

Author Comment

by:peter-cooper
ID: 39653089
@alok
made some minor changes and need your confirmation that although this is working, it is correct coding.

I made changes to my while statement. Is there an easier way to do this or is this correct? many thanks

While dr.Read()
                ListView1.Items.Add(dr(0).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(2))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(6))

            End While

Open in new window


And my for code:

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged
        Try

            Dim test As ListView.SelectedListViewItemCollection = _
            Me.ListView1.SelectedItems
            Dim item As ListViewItem

            Dim code As String = Nothing
            Dim name As String = Nothing
            Dim address As String = Nothing
            Dim email As String = Nothing

            For Each item In test
                code += item.SubItems(0).Text
                name += item.SubItems(1).Text
                address += item.SubItems(2).Text
                email += item.SubItems(3).Text
            Next

            ' Output the price to TextBox1.
            txtname.Text = (code)
            txtfathername.Text = (name)
            txtaddress.Text = (address)
            txtemail.Text = (email)

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try
    End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:Alok-Agarwal
ID: 39653090
This is perfect. Great.
0
 

Author Closing Comment

by:peter-cooper
ID: 39653154
Thanks very much
0

Featured Post

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question