Solved

vb.net retrieve items from Listview on SelectedIndexChanged

Posted on 2013-11-16
5
1,495 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 500 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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