11 Replies - 3669 Views - Last Post: 16 July 2012 - 12:06 PM Rate Topic: -----

#1 S-John   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 13-July 12

Move ListView Row to a Label

Posted 13 July 2012 - 10:11 AM

I've tried looking this up but nobody has a tutorial that clears this issue up for me.

I have a listview containing 3 columns and a user-selected number of rows. I would like to (on btn_click) add all rows from a this ListView to a Label. Here's the catch...I would like to check these items against the items in a database and pull some more information about these parts along with the info that is already in the ListView.

But because the first column in my ListView is an Item (not subitem) and for other reasons I do not understand, the output label only displays the second column.
This is what I have so far:

 Dim counter As Integer
    Private Sub AddPLCbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddPLCbtn.Click
        'Checks for which items have been entered on the PLC tab
        'CheckforEnteredPLC()
        '************
        If PLCTableList.CheckedItems.Count = 1 Then
            Select Case counter
                Case 1
                    lblBomEnteredItems.Text = PLCTableList.SelectedItems(-1).Text
            End Select
            counter += 1
            'This bit of code is so that you loop round and start at 1 again
            If counter > 1 Then
                counter = 1
            End If
        Else
            MsgBox("You must select an item in order to continue")
        End If
        '**************
        iAddedIOGroupID = iAddedIOGroupID + 1
        With Me.PLCTableList
            Dim i As Integer
            For Each item As ListViewItem In PLCTableList.CheckedItems
                i = item.Index
            Next

            Dim innercounter As Integer = 0
            For Each subItem As ListViewItem.ListViewSubItem In PLCTableList.Items(i).SubItems
                Dim myString As String = PLCTableList.Items(i).SubItems(innercounter).Text
                Select Case innercounter
                    Case 1
                        lblBomEnteredItems.Text = myString
                End Select
                innercounter += 1
            Next
        End With
end sub




Thank you for any help you may offer :)

Darn...I messed up the code tags. Here is the code:

 Dim counter As Integer
    Private Sub AddPLCbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddPLCbtn.Click
        'Checks for which items have been entered on the PLC tab
        'CheckforEnteredPLC()
        '************
        If PLCTableList.CheckedItems.Count = 1 Then
            Select Case counter
                Case 1
                    lblBomEnteredItems.Text = PLCTableList.SelectedItems(-1).Text
            End Select
            counter += 1
            'This bit of code is so that you loop round and start at 1 again
            If counter > 1 Then
                counter = 1
            End If
        Else
            MsgBox("You must select an item in order to continue")
        End If
        '**************
        iAddedIOGroupID = iAddedIOGroupID + 1
        With Me.PLCTableList
            Dim i As Integer
            For Each item As ListViewItem In PLCTableList.CheckedItems
                i = item.Index
            Next

            Dim innercounter As Integer = 0
            For Each subItem As ListViewItem.ListViewSubItem In PLCTableList.Items(i).SubItems
                Dim myString As String = PLCTableList.Items(i).SubItems(innercounter).Text
                Select Case innercounter
                    Case 1
                        lblBomEnteredItems.Text = myString
                End Select
                innercounter += 1
            Next
        End With
end sub


This post has been edited by modi123_1: 13 July 2012 - 10:11 AM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Move ListView Row to a Label

#2 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Move ListView Row to a Label

Posted 13 July 2012 - 12:19 PM

how do you mean add all rows from ListView to a Label?
Was This Post Helpful? 0
  • +
  • -

#3 S-John   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 13-July 12

Re: Move ListView Row to a Label

Posted 13 July 2012 - 01:21 PM

First of all I apologize because I am fairly new to VB.NET. I took a course in VB and a course in SQL but mixing the two is strange to me.

It may be more simple than that now that I think about it. I would need to compare the unique identifier(first column in each row of the ListView)to the database, and populate the label with all data associated with each of these unique identifiers.

For example the first row in the ListView could represent a Part_Number (unique identifier). Using this part number I would gather all data about the product and populate the label with this information. I would have to do this in a loop I suppose to step through each row in the listview.

I hope this explains it a bit better and more logically.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Move ListView Row to a Label

Posted 13 July 2012 - 07:44 PM

ok, i see from your code that you know how to step through each item in your listview.

Quote

I would need to compare the unique identifier(first column in each row of the ListView)to the database, and populate the label with all data associated with each of these unique identifiers. For example the first row in the ListView could represent a Part_Number (unique identifier). Using this part number I would gather all data about the product and populate the label with this information.

So you don't know how to gather data from database using unique identifier? it's simple, but first of all you will need to learn how to:
1. create connection to your database
2. create SQL query
3. use SqlCommand
4. use Data Adapter
tell me which of these parts are not familiar to you
Was This Post Helpful? 0
  • +
  • -

#5 S-John   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 13-July 12

Re: Move ListView Row to a Label

Posted 16 July 2012 - 07:34 AM

View Postsela007, on 13 July 2012 - 07:44 PM, said:

So you don't know how to gather data from database using unique identifier? it's simple, but first of all you will need to learn how to:
1. create connection to your database
2. create SQL query
3. use SqlCommand
4. use Data Adapter
tell me which of these parts are not familiar to you


I do have examples of these to reference but my issue lies in doing this within my loop which I have changed to:
With Me.PLCTableList
            'Dim i As Integer
            For Each tmpItem As ListViewItem In PLCTableList.CheckedItems
                Dim myString As String = tmpItem.Text
                For Each tmpSubItem As ListViewItem.ListViewSubItem In tmpItem.SubItems
                    myString &= tmpSubItem.Text
                Next
                lblBomEnteredItems.Text &= myString
            Next
        End With



That is, I do not know how to use only the first column data (my unique identifier) to compare and pull more info from the DB with.

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Move ListView Row to a Label

Posted 16 July 2012 - 07:46 AM

Quote

That is, I do not know how to use only the first column data (my unique identifier) to compare and pull more info from the DB with.

the value from the first column lies in in ListViewItem.Text (tmpItem.Text). All other columns are subitems.
Was This Post Helpful? 0
  • +
  • -

#7 S-John   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 13-July 12

Re: Move ListView Row to a Label

Posted 16 July 2012 - 07:53 AM

View Postsela007, on 16 July 2012 - 07:46 AM, said:

the value from the first column lies in in ListViewItem.Text (tmpItem.Text). All other columns are subitems.


When I debug and test the output to the label it lists the 1st column twice. Maybe recognizing the first column as an item and subitem at the same time?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Move ListView Row to a Label

Posted 16 July 2012 - 08:01 AM

Quote

Maybe recognizing the first column as an item and subitem at the same time?
Now I'm not sure, but if this is true then just set
Dim myString As String = "" 

instead
Dim myString As String = tmpItem.Text

Was This Post Helpful? 1
  • +
  • -

#9 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Move ListView Row to a Label

Posted 16 July 2012 - 08:08 AM

I've just checked. When you use for/each subitem in lvItem then first column is included also even it's not subitem.
Was This Post Helpful? 1
  • +
  • -

#10 S-John   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 13-July 12

Re: Move ListView Row to a Label

Posted 16 July 2012 - 09:48 AM

Private Sub AddPLCbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddPLCbtn.Click
        'Checks for which items have been entered on the PLC tab
        With Me.PLCTableList

            For Each tmpItem As ListViewItem In PLCTableList.CheckedItems
                Dim myString As String = ""

                Try
                    Using udtconn As New SqlConnection(sBoMConnectionString)

                        Dim daPLC As New SqlDataAdapter(New SqlCommand("Select * from PLC where Part_Number = '" & tmpItem.Text & "'", udtconn))
                        Dim dsPLC As New DataSet

                        dsPLC.Tables.Add("PLC")
                        daPLC.Fill(dsPLC.Tables("PLC"))

                        Me.BoMDataSetBindingSource.DataSource = dsPLC.Tables("PLC")
                        daPLC.Fill(dsPLC)

                        'Creates new plc class with the information that was pulled from the database
                        Dim newPLC As New clsPLC()
                        newPLC.PartType() = CType(dsPLC.Tables("PLC").Rows(0).Item("Part_Type"), String)
                        newPLC.PartSubType() = CType(dsPLC.Tables("PLC").Rows(0).Item("Part_Sub_Type"), String)
                        newPLC.PLCPartNumber = CType(dsPLC.Tables("PLC").Rows(0).Item("Part_Number"), String)
                        newPLC.PLCManufacturer = CType(dsPLC.Tables("PLC").Rows(0).Item("Manufacturer"), String)
                        newPLC.PLCVendor = CType(dsPLC.Tables("PLC").Rows(0).Item("Vendor"), String)
                        newPLC.PLCDescription = CType(dsPLC.Tables("PLC").Rows(0).Item("Description"), String)
                        'newPLC.PLCQuantity = tmpitem.subitem.tostring
                        newPLC.PLCPrice = CType(dsPLC.Tables("PLC").Rows(0).Item("Price"), Double)
                        newPLC.AddedGroupID = iAddedIOGroupID

                        lstPLC.Add(newPLC)
                    End Using
                Catch ex As Exception
                    'An error occured while pulling data
                End Try

            Next
        End With

        '   Load onto BoM
        LoadCurrentBoM()

        'Clears the plc tab
        btnClearPLC_Click(Nothing, Nothing)
    End Sub



This is what I have now. It pulls data from the database based upon the unique identifier in the checked row and the LoadCurrentBoM() method organizes it within the label.

Now my issue is with the commented out "newPLC" row. How may I display the subitem QTY (third column in the ListView) in the label as it is in the ListView? With generalized naming(tmpitem.subitem) how might this work?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#11 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Move ListView Row to a Label

Posted 16 July 2012 - 11:56 AM

Quote

Now my issue is with the commented out "newPLC" row. How may I display the subitem QTY (third column in the ListView) in the label as it is in the ListView? With generalized naming(tmpitem.subitem) how might this work?
Sorry I do not understand well, do you mean how to get value of third subitem in tmpItem?
newPLC.PLCQuantity = tmpItem.SubItems(2).Text
is this is what you looking for?
Was This Post Helpful? 1
  • +
  • -

#12 S-John   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 13-July 12

Re: Move ListView Row to a Label

Posted 16 July 2012 - 12:06 PM

View Postsela007, on 16 July 2012 - 11:56 AM, said:

is this is what you looking for?

Yes it was what I was looking for. My knowlege of properties in this language is yet very limited.

Thank you for all of your help sela007!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1