Results 1 to 5 of 5

Thread: [RESOLVED] Populate a listview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Resolved [RESOLVED] Populate a listview

    What code do I use to populate a listview, and how would I handle when a button is clicked, which item in the list view has been selected?

    Cheers.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Populate a listview

    Listbox1.Items.Add("Whatever")
    Listbox1.SelectedValue

  3. #3
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Populate a listview

    Quote Originally Posted by wild_bill
    Listbox1.Items.Add("Whatever")
    Listbox1.SelectedValue

    what wild_bill gave will work but thats just like a cumb of the whole pie...

    I'll be back in a flash to give you a little more code that might be more helpful.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  4. #4
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Populate a listview

    ok here some code... don't worry about some of the code because they are functons that i have created for the project that i'm working on. The code here will help you with the loading/adding of items to the listview... I'll post a second post with the handling which is selected.

    VB Code:
    1. Friend Sub LoadProjects()
    2.         Dim cn As New OleDbConnection(GetOleDbConnection)
    3.         Dim cmd As New OleDbCommand("SELECT colldev_projects.*, colldev_users.fname, colldev_users.lname FROM colldev_users INNER JOIN colldev_projects ON colldev_users.id = colldev_projects.manager_id")
    4.         Dim dr As OleDbDataReader
    5.  
    6.         cmd.Connection = cn
    7.         cmd.CommandTimeout = GetCommandTimeOut()
    8.         cn.Open()
    9.         If cn.State = ConnectionState.Open Then
    10.             dr = cmd.ExecuteReader
    11.             If dr.HasRows Then
    12.                 Me.lvProjects.SuspendLayout()
    13.                 Me.lvProjects.Items.Clear()
    14.                 Do Until Not dr.Read
    15.                     Application.DoEvents()
    16.                     Dim lvitm As New ListViewItem
    17.  
    18.                     With lvitm
    19.                         If CBool(dr("inactive")) Then
    20.                             .BackColor = Color.Yellow
    21.                         End If
    22.                         .Tag = CStr(dr("projid"))
    23.                         .Text = CStr(dr("library_name"))
    24.                         With .SubItems
    25.                             .Add(CStr(dr("fname")) + " " + CStr(dr("lname")))
    26.                             .Add(CStr(dr("start_date")))
    27.                             .Add(CStr(dr("complete_date")))
    28.                             .Add(Format(CDbl(dr("est_num_items")), "###,###,###,###,##0"))
    29.                             .Add(Format(CDbl(dr("proj_amt")), "###,###,###,###,##0.00"))
    30.                             .Add(CBool(dr("inactive")).ToString)
    31.                             .Add(CStr(dr("projid")))
    32.                             .Add(CStr(dr("manager_id")))
    33.                         End With
    34.                     End With
    35.                     Me.lvProjects.Items.Add(lvitm)
    36.                     lvitm = Nothing
    37.                 Loop
    38.                 Me.lvProjects.ResumeLayout()
    39.             End If
    40.         End If
    41.  
    42.         dr.Close()
    43.         cn.Close()
    44.         cmd.Dispose()
    45.         cn.Dispose()
    46.         dr = Nothing
    47.         cmd = Nothing
    48.         cn = Nothing
    49.     End Sub

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  5. #5
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Populate a listview

    take notice to the My.lvProjects.SelectedItems(0).Tag...

    by using the SelectedItems collection you get access to all items that would be selected in the listview. this is helpful for when have the listview allowed to have multiple selection. then just loop through the items in the selecteditems collection

    VB Code:
    1. Private Sub btnEdit_Click(ByVal sender As System.Object, _
    2.         ByVal e As System.EventArgs) Handles btnEdit.Click
    3.         '*******************************************************************************************
    4.         Cursor = Cursors.WaitCursor
    5.         Dim fcp As New frmCreateProject(CStr(Me.lvProjects.SelectedItems(0).Tag))
    6.         AddHandler fcp.ProjectSaved, AddressOf RefreshProjectView
    7.  
    8.         With fcp
    9.             fcp.Text = "Edit Project"
    10.             Cursor = Cursors.Default
    11.             If .ShowDialog(Me) = DialogResult.OK Then
    12.                 Me.btnEdit.Enabled = False
    13.                 Me.btnDelete.Enabled = False
    14.                 Me.btnAct.Enabled = False
    15.                 Me.btnDeAct.Enabled = False
    16.                 Me.btnManager.Enabled = False
    17.                 Me.lvProjects.SelectedItems.Clear()
    18.             End If
    19.         End With
    20.         fcp = Nothing
    21.     End Sub



    here is the code for looping through the collection

    VB Code:
    1. For Each itm As ListViewItem In Me.lvProjects.SelectedItems
    2.             Application.DoEvents()
    3.             MsgBox(itm.Tag)
    4.  
    5.             MsgBox(itm.Text)
    6.             MsgBox(itm.SubItems(0).Text) '<-- this is the same as the previous line.
    7.             MsgBox(itm.SubItems(1).Text) '<-- if you have subitems.
    8.             '.
    9.             '.
    10.             '.
    11.         Next


    have a nice day

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

Posting Permissions

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



Click Here to Expand Forum to Full Width