[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

LISTVIEW nightmare with VB2005

Posted on 2006-06-12
9
Medium Priority
?
577 Views
Last Modified: 2011-09-20
Experts,

I am in desperate need of your assistance.  I have pulled just about all of my hair out trying to get this listview to work.  My project goal is to fill the listview from a database and then fill a textbox with the checked value of the listview.  Below is my code:

    Private Sub TESTING_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim conn2 As New System.Data.SqlClient.SqlConnection("Persist Security Info=True;Server=127.0.0.1;Database=Reporting;uid=sa;network=dbmssocn;password=******")
        Dim da2 As New System.Data.SqlClient.SqlDataAdapter("Select * from tblPeople", conn2)
        Dim ds2 As New System.Data.DataSet
        Dim cmd As New System.Data.SqlClient.SqlCommand("SELECT * from tbldrug where arn='" & OffenseIncidentEdit.AgencyReportNumber.Text & "' and Incident_ID='" & OffenseIncidentEdit.Incident_ID.Text & "' and deleted='0' order by dateentered", conn2)
       
        Try
            conn2.Open()
            Dim myReader As SqlDataReader = cmd.ExecuteReader()

            Do While myReader.Read()


                Dim lvi As New ListViewItem(myReader.Item("Activity").ToString)                 ' This works as far as populating the listview
                lvi.SubItems.Add(myReader.Item("Type").ToString())                                  ' This works as far as populating the listview
                lvi.SubItems.Add(myReader.Item("Description").ToString())                         ' This works as far as populating the listview
                lvi.SubItems.Add(myReader.Item("drug_id").ToString())                              ' This works as far as populating the listview (I need this value to populate textbox1 when the selected item is clicked)
                ListView1.Items.Add(lvi)

            Loop

             myReader.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally

        End Try
        conn2.Close()

    End Sub

My goal is to on click, fill textbox1.text with the selected value "DRUG_ID"

Please help
0
Comment
Question by:NCSO
[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
  • 6
  • 2
9 Comments
 

Author Comment

by:NCSO
ID: 16888312
AM I missing the SelectedListViewItemCollection Class?  I am at a lose!
0
 
LVL 9

Expert Comment

by:dancebert
ID: 16888507
You will probably get better answers if you post this question in the VB.NET area.
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16888513
Hi, you'll need to handle the "SelectedIndexChanged" event for the ListView control. Do this by selecting the ListView in design view, in the properties pane select the icon which has a lightning bolt graphic, double click the event named SelectedIndexChanged. This should take you into the code editor and should automatically generate the event handling procedure. In the end you should have something like:

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

        Me.TextBox1.Text = Me.ListView1.FocusedItem.SubItems(3).Text
    End Sub

The literal value 3 is the column index...

Hope this helps!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NCSO
ID: 16888651
Zephyr,

When I click the checkbox, nothing happens.
0
 

Author Comment

by:NCSO
ID: 16888749
if I put it "on leave" or "item checked" it show the first indexed return and no other
0
 

Author Comment

by:NCSO
ID: 16888843
it is as if the focused item is always the first return
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 2000 total points
ID: 16888900
Hi, sorry, use the ItemChecked event instead, try something like:

    Private Sub ListView1_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked

        If Me.ListView1.CheckedItems.Count > 0 Then

            Dim CheckedItem As ListViewItem = Me.ListView1.CheckedItems(0)
            Me.TextBox1.Text = CheckedItem.SubItems(3).Text
        End If
    End Sub

If multiple checkboxes are checked, only the first one checked will be displayed in the textbox, if you want all the items that are checked to be displayed in one textbox then you'll have to iterate the ListView1.CheckedItems collection (using a ForEach loop) and concatenate the string values in the textbox.
0
 

Author Comment

by:NCSO
ID: 16888921
I changed the listview to full row select and it now works but I get the following error when I select a different row:

Object reference not set to an instance of an object.
0
 

Author Comment

by:NCSO
ID: 16888974
THANK YOU VERRRRRY MUCH!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

649 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