Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListView and Inserting Subitems

ListView and Inserting Subitems

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valikac
    wrote on last edited by
    #1

    Hello, How do you append/set subitem within a row? For example given this listview. 0 1 2 3 4 5 1 2 3 4 In row 2, I'd like to add "5" after the "4." Class ListView supports adding a new row via ListViewItem. How do you append a new subitem to an exist row? How do you set the entire row? Thanks, Kuphryn

    J 1 Reply Last reply
    0
    • V valikac

      Hello, How do you append/set subitem within a row? For example given this listview. 0 1 2 3 4 5 1 2 3 4 In row 2, I'd like to add "5" after the "4." Class ListView supports adding a new row via ListViewItem. How do you append a new subitem to an exist row? How do you set the entire row? Thanks, Kuphryn

      J Offline
      J Offline
      Jon G
      wrote on last edited by
      #2

      Here is how I do what you are asking: private System.Windows.Forms.ListView myList; //init ListView object private System.Windows.Forms.ColumnHeader ItemNameCol; //Column Header objects private System.Windows.Forms.ColumnHeader SubItem1Col; private System.Windows.Forms.ColumnHeader SubItem2Col; private System.Windows.Forms.ColumnHeader SubItem3Col; //give the headers some names ItemNameCol.Text = "Name"; SubItem1Col.Text = "Col1"; SubItem2Col.Text = "Col2"; SubItem3Col.Text = "Col3"; //create the columns to store the values using Column header objects this.myList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.ItemNameCol, this.SubItem1Col, this.SubItem2Col, this.SubItem3Col }); //create a item, with 3 subitems ListViewItem item1 = new ListViewItem("ItemName",0); item1.SubItems.Add( "SubItem1Value" ); item1.SubItems.Add( "SubItem2Value" ); item1.SubItems.Add( "SubItem3Value" ); myList.Items.AddRange(new ListViewItem[]{item1}); //add row to the listview Thats how I create a new ListView. To append to a listview, I do the following: item1.SubItems.Add( "New Item Value" ); //insert a new subItem in the list ColumnHeader newHeader = new ColumnHeader(); //create a new header for new subitem newHeader.Text = "New Header"; //give it a name this.myList.Columns.Add(newHeader); //insert new header in list I hope this helps out. The above code is quite crude, considering I wrote it pretty fast. Let me know if you have any questions about it.

      V 1 Reply Last reply
      0
      • J Jon G

        Here is how I do what you are asking: private System.Windows.Forms.ListView myList; //init ListView object private System.Windows.Forms.ColumnHeader ItemNameCol; //Column Header objects private System.Windows.Forms.ColumnHeader SubItem1Col; private System.Windows.Forms.ColumnHeader SubItem2Col; private System.Windows.Forms.ColumnHeader SubItem3Col; //give the headers some names ItemNameCol.Text = "Name"; SubItem1Col.Text = "Col1"; SubItem2Col.Text = "Col2"; SubItem3Col.Text = "Col3"; //create the columns to store the values using Column header objects this.myList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.ItemNameCol, this.SubItem1Col, this.SubItem2Col, this.SubItem3Col }); //create a item, with 3 subitems ListViewItem item1 = new ListViewItem("ItemName",0); item1.SubItems.Add( "SubItem1Value" ); item1.SubItems.Add( "SubItem2Value" ); item1.SubItems.Add( "SubItem3Value" ); myList.Items.AddRange(new ListViewItem[]{item1}); //add row to the listview Thats how I create a new ListView. To append to a listview, I do the following: item1.SubItems.Add( "New Item Value" ); //insert a new subItem in the list ColumnHeader newHeader = new ColumnHeader(); //create a new header for new subitem newHeader.Text = "New Header"; //give it a name this.myList.Columns.Add(newHeader); //insert new header in list I hope this helps out. The above code is quite crude, considering I wrote it pretty fast. Let me know if you have any questions about it.

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        Okay. Thanks. Your solution works great for inserting a new column to an existing list. I was thinking more in terms of inserting (setting) text to an existing column. 0 1 2 3 4 5 1 2 3 4 <-- insert text here. column already exists. Kuphryn

        O 1 Reply Last reply
        0
        • V valikac

          Okay. Thanks. Your solution works great for inserting a new column to an existing list. I was thinking more in terms of inserting (setting) text to an existing column. 0 1 2 3 4 5 1 2 3 4 <-- insert text here. column already exists. Kuphryn

          O Offline
          O Offline
          osto
          wrote on last edited by
          #4

          ListViewItem item = lstProducts.Items.Add("Column1");//<-first item.SubItems.Add("Column2"); item.SubItems.Add("Column3"); item.SubItems.Add("Column4"); item.SubItems.Add("Column5"); easy? ;P and if you want to update a previously created column listName.Items[index].SubItems[subIndex].Text = "";

          V 1 Reply Last reply
          0
          • O osto

            ListViewItem item = lstProducts.Items.Add("Column1");//<-first item.SubItems.Add("Column2"); item.SubItems.Add("Column3"); item.SubItems.Add("Column4"); item.SubItems.Add("Column5"); easy? ;P and if you want to update a previously created column listName.Items[index].SubItems[subIndex].Text = "";

            V Offline
            V Offline
            valikac
            wrote on last edited by
            #5

            Nice!!! You have to love the [] operator. Thanks, Kuphryn

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups