Solved

vb.net retrieve items from Listview on SelectedIndexChanged

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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