Add items programatically to ListView

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    #1

    Add items programatically to ListView

    Using VS2005

    I have added a Listview containing 4 columns in design mode...

    How can i populate it programatically ?

    I tried... but only "111" shows up in the list view. Also, I want it to
    create a new line when adding it.


    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Dim str(4) As String


    Dim itm As ListViewItem


    str(0) = "111"
    str(1) = "894"
    str(2) = "888"
    str(3) = "222"

    itm = New ListViewItem(st r)
    ListView1.Items .Add(itm)

    End Sub


  • Mr. Arnold

    #2
    Re: Add items programatically to ListView


    "Rob" <robc1@yahoo.co mwrote in message
    news:IaqdnWEq_o d0vfnbnZ2dnUVZ_ g-dnZ2d@comcast.c om...
    Using VS2005
    >
    I have added a Listview containing 4 columns in design mode...
    >
    How can i populate it programatically ?
    >
    I tried... but only "111" shows up in the list view. Also, I want it to
    create a new line when adding it.
    >
    >
    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Dim str(4) As String
    >
    >
    Dim itm As ListViewItem
    >
    >
    str(0) = "111"
    str(1) = "894"
    str(2) = "888"
    str(3) = "222"
    >
    itm = New ListViewItem(st r)
    ListView1.Items .Add(itm)
    >
    End Sub
    >
    You do have to walk str(0) through str(3) with some kind of For Loop I
    would think, if that str() is some kind of an array.

    for loop

    itm = New ListViewItem(st r(i))
    ListView1.Items .Add(itm)

    next


    Comment

    • Johnny Jörgensen

      #3
      Re: Add items programatically to ListView

      I think what the OP meant was this:


      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      Dim str(4) As String


      Dim itm As ListViewItem


      str(0) = "111"
      str(1) = "894"
      str(2) = "888"
      str(3) = "222"

      itm = New ListViewItem(st r(0))
      ListView1.Items .Add(itm)
      ListView1.SubIt ems.Add(str(1))
      ListView1.SubIt ems.Add(str(2))
      ListView1.SubIt ems.Add(str(3))

      End Sub
      "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
      news:%23QJOP7rp HHA.3524@TK2MSF TNGP02.phx.gbl. ..
      >
      "Rob" <robc1@yahoo.co mwrote in message
      news:IaqdnWEq_o d0vfnbnZ2dnUVZ_ g-dnZ2d@comcast.c om...
      >Using VS2005
      >>
      >I have added a Listview containing 4 columns in design mode...
      >>
      >How can i populate it programatically ?
      >>
      >I tried... but only "111" shows up in the list view. Also, I want it to
      >create a new line when adding it.
      >>
      >>
      >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      >System.EventAr gs) Handles Button1.Click
      > Dim str(4) As String
      >>
      >>
      > Dim itm As ListViewItem
      >>
      >>
      > str(0) = "111"
      > str(1) = "894"
      > str(2) = "888"
      > str(3) = "222"
      >>
      > itm = New ListViewItem(st r)
      > ListView1.Items .Add(itm)
      >>
      > End Sub
      >>
      >
      You do have to walk str(0) through str(3) with some kind of For Loop I
      would think, if that str() is some kind of an array.
      >
      for loop
      >
      itm = New ListViewItem(st r(i))
      ListView1.Items .Add(itm)
      >
      next
      >
      >

      Comment

      • PlatinumBay

        #4
        Re: Add items programatically to ListView

        Rob,

        Make sure the list view is in Details mode.
        ListView1.View = System.Windows. Forms.View.Deta ils

        Hope this helps,


        Steve

        "Rob" <robc1@yahoo.co mwrote in message
        news:IaqdnWEq_o d0vfnbnZ2dnUVZ_ g-dnZ2d@comcast.c om...
        Using VS2005
        >
        I have added a Listview containing 4 columns in design mode...
        >
        How can i populate it programatically ?
        >
        I tried... but only "111" shows up in the list view. Also, I want it to
        create a new line when adding it.
        >
        >
        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
        System.EventArg s) Handles Button1.Click
        Dim str(4) As String
        >
        >
        Dim itm As ListViewItem
        >
        >
        str(0) = "111"
        str(1) = "894"
        str(2) = "888"
        str(3) = "222"
        >
        itm = New ListViewItem(st r)
        ListView1.Items .Add(itm)
        >
        End Sub
        >

        Comment

        • Rob

          #5
          Re: Add items programatically to ListView

          Johnny,

          I think I am missing something because "SubItems" code does not work...

          Thanks,
          Rob



          "Johnny Jörgensen" <jojo@altcom.se wrote in message
          news:uDMMd8rpHH A.2652@TK2MSFTN GP02.phx.gbl...
          >I think what the OP meant was this:
          >
          >
          Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          System.EventArg s) Handles Button1.Click
          Dim str(4) As String
          >
          >
          Dim itm As ListViewItem
          >
          >
          str(0) = "111"
          str(1) = "894"
          str(2) = "888"
          str(3) = "222"
          >
          itm = New ListViewItem(st r(0))
          ListView1.Items .Add(itm)
          ListView1.SubIt ems.Add(str(1))
          ListView1.SubIt ems.Add(str(2))
          ListView1.SubIt ems.Add(str(3))
          >
          End Sub
          "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
          news:%23QJOP7rp HHA.3524@TK2MSF TNGP02.phx.gbl. ..
          >>
          >"Rob" <robc1@yahoo.co mwrote in message
          >news:IaqdnWEq_ od0vfnbnZ2dnUVZ _g-dnZ2d@comcast.c om...
          >>Using VS2005
          >>>
          >>I have added a Listview containing 4 columns in design mode...
          >>>
          >>How can i populate it programatically ?
          >>>
          >>I tried... but only "111" shows up in the list view. Also, I want it
          >>to create a new line when adding it.
          >>>
          >>>
          >>Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          >>System.EventA rgs) Handles Button1.Click
          >> Dim str(4) As String
          >>>
          >>>
          >> Dim itm As ListViewItem
          >>>
          >>>
          >> str(0) = "111"
          >> str(1) = "894"
          >> str(2) = "888"
          >> str(3) = "222"
          >>>
          >> itm = New ListViewItem(st r)
          >> ListView1.Items .Add(itm)
          >>>
          >> End Sub
          >>>
          >>
          >You do have to walk str(0) through str(3) with some kind of For Loop I
          >would think, if that str() is some kind of an array.
          >>
          >for loop
          >>
          >itm = New ListViewItem(st r(i))
          >ListView1.Item s.Add(itm)
          >>
          >next
          >>
          >>
          >
          >

          Comment

          • Rob

            #6
            Re: Add items programatically to ListView

            Oh I think I see the problem...

            ListView1.SubIt ems.Add(str(1))
            ListView1.SubIt ems.Add(str(2))
            ListView1.SubIt ems.Add(str(3))

            should be...

            itm.SubItems.Ad d(str(1))
            itm.SubItems.Ad d(str(2))
            itm.SubItems.Ad d(str(3))

            Thanks

            "Rob" <robc1@yahoo.co mwrote in message
            news:Xuidnaane8 3zFvnbnZ2dnUVZ_ q-vnZ2d@comcast.c om...
            Johnny,
            >
            I think I am missing something because "SubItems" code does not work...
            >
            Thanks,
            Rob
            >
            >
            >
            "Johnny Jörgensen" <jojo@altcom.se wrote in message
            news:uDMMd8rpHH A.2652@TK2MSFTN GP02.phx.gbl...
            >>I think what the OP meant was this:
            >>
            >>
            >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
            >System.EventAr gs) Handles Button1.Click
            > Dim str(4) As String
            >>
            >>
            > Dim itm As ListViewItem
            >>
            >>
            > str(0) = "111"
            > str(1) = "894"
            > str(2) = "888"
            > str(3) = "222"
            >>
            > itm = New ListViewItem(st r(0))
            > ListView1.Items .Add(itm)
            > ListView1.SubIt ems.Add(str(1))
            > ListView1.SubIt ems.Add(str(2))
            > ListView1.SubIt ems.Add(str(3))
            >>
            > End Sub
            >"Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
            >news:%23QJOP7r pHHA.3524@TK2MS FTNGP02.phx.gbl ...
            >>>
            >>"Rob" <robc1@yahoo.co mwrote in message
            >>news:IaqdnWEq _od0vfnbnZ2dnUV Z_g-dnZ2d@comcast.c om...
            >>>Using VS2005
            >>>>
            >>>I have added a Listview containing 4 columns in design mode...
            >>>>
            >>>How can i populate it programatically ?
            >>>>
            >>>I tried... but only "111" shows up in the list view. Also, I want it
            >>>to create a new line when adding it.
            >>>>
            >>>>
            >>>Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
            >>>System.Event Args) Handles Button1.Click
            >>> Dim str(4) As String
            >>>>
            >>>>
            >>> Dim itm As ListViewItem
            >>>>
            >>>>
            >>> str(0) = "111"
            >>> str(1) = "894"
            >>> str(2) = "888"
            >>> str(3) = "222"
            >>>>
            >>> itm = New ListViewItem(st r)
            >>> ListView1.Items .Add(itm)
            >>>>
            >>> End Sub
            >>>>
            >>>
            >>You do have to walk str(0) through str(3) with some kind of For Loop I
            >>would think, if that str() is some kind of an array.
            >>>
            >>for loop
            >>>
            >>itm = New ListViewItem(st r(i))
            >>ListView1.Ite ms.Add(itm)
            >>>
            >>next
            >>>
            >>>
            >>
            >>
            >
            >

            Comment

            • Johnny Jörgensen

              #7
              Re: Add items programatically to ListView

              True

              Sorry - my mistake. I copied your code and forgot to change that...

              Cheers,
              Johnny J.





              "Rob" <robc1@yahoo.co mwrote in message
              news:avCdnTIhQs a0EvnbnZ2dnUVZ_ qWvnZ2d@comcast .com...
              Oh I think I see the problem...
              >
              ListView1.SubIt ems.Add(str(1))
              ListView1.SubIt ems.Add(str(2))
              ListView1.SubIt ems.Add(str(3))
              >
              should be...
              >
              itm.SubItems.Ad d(str(1))
              itm.SubItems.Ad d(str(2))
              itm.SubItems.Ad d(str(3))
              >
              Thanks
              >
              "Rob" <robc1@yahoo.co mwrote in message
              news:Xuidnaane8 3zFvnbnZ2dnUVZ_ q-vnZ2d@comcast.c om...
              >Johnny,
              >>
              >I think I am missing something because "SubItems" code does not work...
              >>
              >Thanks,
              >Rob
              >>
              >>
              >>
              >"Johnny Jörgensen" <jojo@altcom.se wrote in message
              >news:uDMMd8rpH HA.2652@TK2MSFT NGP02.phx.gbl.. .
              >>>I think what the OP meant was this:
              >>>
              >>>
              >>Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
              >>System.EventA rgs) Handles Button1.Click
              >> Dim str(4) As String
              >>>
              >>>
              >> Dim itm As ListViewItem
              >>>
              >>>
              >> str(0) = "111"
              >> str(1) = "894"
              >> str(2) = "888"
              >> str(3) = "222"
              >>>
              >> itm = New ListViewItem(st r(0))
              >> ListView1.Items .Add(itm)
              >> ListView1.SubIt ems.Add(str(1))
              >> ListView1.SubIt ems.Add(str(2))
              >> ListView1.SubIt ems.Add(str(3))
              >>>
              >> End Sub
              >>"Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
              >>news:%23QJOP7 rpHHA.3524@TK2M SFTNGP02.phx.gb l...
              >>>>
              >>>"Rob" <robc1@yahoo.co mwrote in message
              >>>news:IaqdnWE q_od0vfnbnZ2dnU VZ_g-dnZ2d@comcast.c om...
              >>>>Using VS2005
              >>>>>
              >>>>I have added a Listview containing 4 columns in design mode...
              >>>>>
              >>>>How can i populate it programatically ?
              >>>>>
              >>>>I tried... but only "111" shows up in the list view. Also, I want it
              >>>>to create a new line when adding it.
              >>>>>
              >>>>>
              >>>>Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
              >>>>System.Even tArgs) Handles Button1.Click
              >>>> Dim str(4) As String
              >>>>>
              >>>>>
              >>>> Dim itm As ListViewItem
              >>>>>
              >>>>>
              >>>> str(0) = "111"
              >>>> str(1) = "894"
              >>>> str(2) = "888"
              >>>> str(3) = "222"
              >>>>>
              >>>> itm = New ListViewItem(st r)
              >>>> ListView1.Items .Add(itm)
              >>>>>
              >>>> End Sub
              >>>>>
              >>>>
              >>>You do have to walk str(0) through str(3) with some kind of For Loop I
              >>>would think, if that str() is some kind of an array.
              >>>>
              >>>for loop
              >>>>
              >>>itm = New ListViewItem(st r(i))
              >>>ListView1.It ems.Add(itm)
              >>>>
              >>>next
              >>>>
              >>>>
              >>>
              >>>
              >>
              >>
              >
              >

              Comment

              Working...