locked
ListView: How to add items with subitems? RRS feed

  • Question

  • How can I add an item with 2 subitems in a listview with a click of a button?



    I have 3 textboxes(name, age and gender) and i would like a button to make a new item in a listview which has 3 columns(name, age and gender).

    Thank you
    Friday, July 31, 2009 4:26 PM

Answers

  • This assumes you have 3 text boxes called txtName, txtAge and txtGender on the form with a ListView control named ListView1.  This creates a 'ListViewItem' object, sets it's text and then adds to sub items (that need to be in the order that they appear in your listview).. I also assume that you've setup the columns in the ListView already:

            Dim lvi As New ListViewItem(txtName.Text)
            lvi.SubItems.Add(txtGender.Text)
            lvi.SubItems.Add(txtAge.Text)
    
            ListView1.Items.Add(lvi)
    • Edited by bpell Friday, July 31, 2009 4:58 PM
    • Proposed as answer by bpell Saturday, August 1, 2009 6:54 PM
    • Marked as answer by Murt2003 Sunday, August 2, 2009 9:34 PM
    Friday, July 31, 2009 4:57 PM
  • Sure.  Drop a label onto your form and leave it the default name of Label1 (or make sure it's named Label1).  Then, put this in a button code.  We know position of the age column in the ListView and it's one of the sub items.  So, we're going to loop through each item in the ListView and keep a running total of the ages, then set the Label value like this:

            Dim ageTotal As Integer = 0
    
            For Each lvi As ListViewItem In ListView1.Items
                If IsNumeric(lvi.SubItems(2).Text.ToString) = True Then
                    ageTotal += CType(lvi.SubItems(2).Text.ToString, Integer)
                End If
            Next
    
            Label1.Text = "Sum of ages = " & ageTotal.ToString
    
    • Marked as answer by Murt2003 Sunday, August 2, 2009 9:34 PM
    Saturday, August 1, 2009 6:54 PM

All replies

  • This assumes you have 3 text boxes called txtName, txtAge and txtGender on the form with a ListView control named ListView1.  This creates a 'ListViewItem' object, sets it's text and then adds to sub items (that need to be in the order that they appear in your listview).. I also assume that you've setup the columns in the ListView already:

            Dim lvi As New ListViewItem(txtName.Text)
            lvi.SubItems.Add(txtGender.Text)
            lvi.SubItems.Add(txtAge.Text)
    
            ListView1.Items.Add(lvi)
    • Edited by bpell Friday, July 31, 2009 4:58 PM
    • Proposed as answer by bpell Saturday, August 1, 2009 6:54 PM
    • Marked as answer by Murt2003 Sunday, August 2, 2009 9:34 PM
    Friday, July 31, 2009 4:57 PM
  • You assume everything correct and I appreciate your help. This way it works.

    Do you also happen to know how I could get all the ages from the items in the listview and for example sum the ages up and display with a label?

    Thank you!
    • Marked as answer by Murt2003 Sunday, August 2, 2009 9:34 PM
    • Unmarked as answer by Murt2003 Sunday, August 2, 2009 9:34 PM
    Friday, July 31, 2009 6:46 PM
  • Sure.  Drop a label onto your form and leave it the default name of Label1 (or make sure it's named Label1).  Then, put this in a button code.  We know position of the age column in the ListView and it's one of the sub items.  So, we're going to loop through each item in the ListView and keep a running total of the ages, then set the Label value like this:

            Dim ageTotal As Integer = 0
    
            For Each lvi As ListViewItem In ListView1.Items
                If IsNumeric(lvi.SubItems(2).Text.ToString) = True Then
                    ageTotal += CType(lvi.SubItems(2).Text.ToString, Integer)
                End If
            Next
    
            Label1.Text = "Sum of ages = " & ageTotal.ToString
    
    • Marked as answer by Murt2003 Sunday, August 2, 2009 9:34 PM
    Saturday, August 1, 2009 6:54 PM