dcsimg
Results 1 to 10 of 10

Thread: Adding items to a listview

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Adding items to a listview

    Sorry about this silly question but I haven't done any Winforms work or VB in ages.
    I am looping through a collection and adding items to a listvew control with the following code:
    Code:
     For Each f As FDayResult In failedResults
                Dim item1 As New ListViewItem("lstItem")
                item1.SubItems.Add(f.QTY.ToString())
                item1.SubItems.Add(f.ProductCode)
                ListView1.Items.Add(item1)
            Next
    All my listview shows is : item1 item1item1item1 in the horizontal.

    Hmmmmmmm
    I'm embaressed.

    Parksie

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,719

    Re: Adding items to a listview

    does it show "item1" or "lstItem" ??? if it shows "lstItem"... then sure... because that's what you set as the text when you created it. Presumably you want some other value, like one of the properties of f.

    Now, if what you meant is why you aren't seeing the subitems, then it's because you're in one of the icon modes - which is the default. Try switching it to Report or Detail (it used to be one, now it's called the other... I don't remember which is the old name or the new name, but only one of those two should be available as the mode in the properties window... choose wisely).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Adding items to a listview

    I think you'll want to actually instantiate your sub items so you can assign the Text property. There may be a better solution but this works...
    Code:
     Dim item1 As New ListViewItem("lstItem")
            For i As Integer = 1 To 5
                Dim si As New ListViewItem.ListViewSubItem()
                si.Text = "Qty" & i
                item1.SubItems.Add(si)
            Next
            ListView1.Items.Add(item1)
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  4. #4
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Adding items to a listview

    Quote Originally Posted by techgnome View Post
    does it show "item1" or "lstItem" ??? if it shows "lstItem"... then sure... because that's what you set as the text when you created it. Presumably you want some other value, like one of the properties of f.

    Now, if what you meant is why you aren't seeing the subitems, then it's because you're in one of the icon modes - which is the default. Try switching it to Report or Detail (it used to be one, now it's called the other... I don't remember which is the old name or the new name, but only one of those two should be available as the mode in the properties window... choose wisely).

    -tg

    technome is correct, Details view and you'll need to add columns to the listview when you switch to this view type. 1 column for the main item and 1 column for each subitem.
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  5. #5

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Adding items to a listview

    Thanks for that guys but to be honest I am not sure what you mean. I have set the view to Details mode but now My column heqders just show "Column 1, Column 2" e.t.cand the first column shows "lstitem" and not f.qty.tostring() which appears under "Column 1".

    I have used the designer to add columns but what do I need to do next?
    Last edited by venerable bede; Mar 20th, 2014 at 04:22 AM.

    Parksie

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,940

    Re: Adding items to a listview

    the lv has a View property that you need to set to Details

  7. #7

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Adding items to a listview

    Ooops.
    I think I edited my post while you where answering.
    I think my problem is that I dont want the parent . All I want is a plain old list of items with column headers but can figure out how to achieve this.

    Parksie

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,940

    Re: Adding items to a listview

    you can edit the columnHeader Text in the designer, just right click the lv + each columnHeader has a Text property

    Code:
    For Each f As FDayResult In failedResults
        Dim item1 As New ListViewItem(f.QTY.ToString())
        item1.SubItems.Add(f.ProductCode)
        ListView1.Items.Add(item1)
    Next

  9. #9
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Adding items to a listview

    I think this will get you to the path you want to get to. I was surprised to find out that when I added the "1stItem" as my ListViewItem it also took on the position of the first subitem at index 0.

    Code:
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim item1 As New ListViewItem("lstItem")
            Dim si As ListViewItem.ListViewSubItem
            Me.ListView1.View = View.Details
    
            Me.ListView1.Columns.Add("AnItem")
            Me.ListView1.Columns(0).Width = 0  'Hide the column of the 1stItem
            Me.ListView1.Columns.Add("Quantity", 50)
            Me.ListView1.Columns.Add("Product Code", 200)
    
            si = New ListViewItem.ListViewSubItem
            si.Text = "Qty"
            item1.SubItems.Add(si)
    
            si = New ListViewItem.ListViewSubItem
            si.Text = "ProductCode"
            item1.SubItems.Add(si)
    
            For i As Integer = 1 To 5
                Dim item As New ListViewItem
                item = item1.Clone
                item.SubItems(1).Text = item.SubItems(1).Text & i.ToString
                item.SubItems(2).Text = item.SubItems(2).Text & i.ToString
                ListView1.Items.Add(item)
            Next
    
        End Sub
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  10. #10
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Adding items to a listview

    ... thinking more about it I wanted to give you an example more to your actual effort...

    Code:
     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'Test Data
            Dim thedata As New DataTable
            thedata = MakeFakeTestDataTable()
    
            'Setup the ListView
            Me.ListView1.View = View.Details
            Me.ListView1.Columns.Add("AnItem")
            Me.ListView1.Columns(0).Width = 0  'Hide the column of the 1stItem
            Me.ListView1.Columns.Add("Qtty", 50).Name = "Quantity"
            Me.ListView1.Columns.Add("Prd Cde", 200).Name = "Product Code"
    
            'Add Items
            For Each dr As DataRow In thedata.Rows
                Dim item1 As New ListViewItem("lstItem")
                Dim si As New ListViewItem.ListViewSubItem
    
                'Quantity
                item1.Text = dr(0)
                si.Text = dr(1).ToString
                item1.SubItems.Add(si)
    
                'Product Code
                si = New ListViewItem.ListViewSubItem
                si.Text = dr(2)
                item1.SubItems.Add(si)
    
                ListView1.Items.Add(item1)
            Next
        End Sub
    
      Private Function MakeFakeTestDataTable() As DataTable
            'Setup fake test data to model venerable bede's data
            Dim dt As New DataTable
            Dim dr As DataRow
    
            dt.Columns.Add("Items")
            dt.Columns.Add("Qty")
            dt.Columns.Add("ProductCode")
    
            dr = dt.NewRow
            dr("Items") = "1stItem"
            dr("Qty") = 9
            dr("ProductCode") = "sdf"
            dt.Rows.Add(dr)
    
            dr = dt.NewRow
            dr("Items") = "2ndItem"
            dr("Qty") = 4
            dr("ProductCode") = "poi"
            dt.Rows.Add(dr)
    
            dr = dt.NewRow
            dr("Items") = "3rdItem"
            dr("Qty") = 7
            dr("ProductCode") = "wer"
            dt.Rows.Add(dr)
    
            dr = dt.NewRow
            dr("Items") = "4thItem"
            dr("Qty") = 2
            dr("ProductCode") = "mnb"
            dt.Rows.Add(dr)
    
            MakeFakeTestDataTable = dt
    
        End Function
    Last edited by thetimmer; Mar 20th, 2014 at 08:14 AM. Reason: Forgot the data
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width