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 Control - Insert Method

ListView Control - Insert Method

Scheduled Pinned Locked Moved C#
databasehelpquestion
5 Posts 2 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.
  • X Offline
    X Offline
    xfitr2
    wrote on last edited by
    #1

    I am having trouble using the ListView.ListItems.Insert() method. When I create a new listview item, and use the Insert method with an index of let's say 0 to be the first item in the listview, it automatically appends to the end of the listview. I've just about given up. Is there a property I need to set or something? Below is a code snippet to give you an idea. Any help would be appreciated. Many thanks. (There is an ImageList associated with the ListView control with at least 3 images.)

    private void Whatever()
    {
    listView1.Items.Add("One", 0);
    listView1.Items.Add("Two", 1);
    listView1.Items.Add("Three", 1);

     ListViewItem lstvwitm = new ListViewItem("Inserted", 2);
     listView1.Items.Insert(0, lstvwwitem);
    

    }

    Garrett

    T 1 Reply Last reply
    0
    • X xfitr2

      I am having trouble using the ListView.ListItems.Insert() method. When I create a new listview item, and use the Insert method with an index of let's say 0 to be the first item in the listview, it automatically appends to the end of the listview. I've just about given up. Is there a property I need to set or something? Below is a code snippet to give you an idea. Any help would be appreciated. Many thanks. (There is an ImageList associated with the ListView control with at least 3 images.)

      private void Whatever()
      {
      listView1.Items.Add("One", 0);
      listView1.Items.Add("Two", 1);
      listView1.Items.Add("Three", 1);

       ListViewItem lstvwitm = new ListViewItem("Inserted", 2);
       listView1.Items.Insert(0, lstvwwitem);
      

      }

      Garrett

      T Offline
      T Offline
      Travis D Mathison
      wrote on last edited by
      #2

      The Insert method will indeed insert at index 0 of the ListView however if you are in Large/Small Icon view mode then by default the "AutoArrange" property will be set to "True" so it's going to sort the item regardless of the fact that you inserted it at index 0. Also check the "Sorting" property of the ListView control to make sure its set to "None" (the default).

      X 1 Reply Last reply
      0
      • T Travis D Mathison

        The Insert method will indeed insert at index 0 of the ListView however if you are in Large/Small Icon view mode then by default the "AutoArrange" property will be set to "True" so it's going to sort the item regardless of the fact that you inserted it at index 0. Also check the "Sorting" property of the ListView control to make sure its set to "None" (the default).

        X Offline
        X Offline
        xfitr2
        wrote on last edited by
        #3

        Thank you for the response. You are absolutely right. I did a Debug.WriteLine(listView1.Items[i].Text) and it printed the ListItems in the order that I was inserting them into the collection. After the insert method, I explicitly changed the properties of AutoArrange and Sorting to false and None, respectively, and it still sorted them (Using Large Icon view). I also tried to change the properties before I used the insert method. Is there any way to stop the ListView from auto-arranging or sort by index number? Thanks. Garrett -- modified at 15:11 Wednesday 3rd May, 2006

        T 1 Reply Last reply
        0
        • X xfitr2

          Thank you for the response. You are absolutely right. I did a Debug.WriteLine(listView1.Items[i].Text) and it printed the ListItems in the order that I was inserting them into the collection. After the insert method, I explicitly changed the properties of AutoArrange and Sorting to false and None, respectively, and it still sorted them (Using Large Icon view). I also tried to change the properties before I used the insert method. Is there any way to stop the ListView from auto-arranging or sort by index number? Thanks. Garrett -- modified at 15:11 Wednesday 3rd May, 2006

          T Offline
          T Offline
          Travis D Mathison
          wrote on last edited by
          #4

          This is a kinda weird one then Garrett.. I've been trying to replicate the same issue here with a ListView using both Large Icon and Small Icon and have not been able to repro the sorting error. The icons and values of the indexes are being placed into the ListView exactly as expected (using the same code you supplied in your first post). If you're still having this issue I would do a line-by-line debug of each insert and step into each "Add" and "Insert" (F11) to see if you can catch where it goes bad --

          X 1 Reply Last reply
          0
          • T Travis D Mathison

            This is a kinda weird one then Garrett.. I've been trying to replicate the same issue here with a ListView using both Large Icon and Small Icon and have not been able to repro the sorting error. The icons and values of the indexes are being placed into the ListView exactly as expected (using the same code you supplied in your first post). If you're still having this issue I would do a line-by-line debug of each insert and step into each "Add" and "Insert" (F11) to see if you can catch where it goes bad --

            X Offline
            X Offline
            xfitr2
            wrote on last edited by
            #5

            Tell me about it. That is why I posted to the forum. I did figure out how to fix the problem yesterday. I created a class that inherits the IComparer interface and then used the ListViewItemSorter to sort by index. This actually worked better because now I can sort it whichever way I want to. I got the idea from this website for anyone who had the same problem I did: http://support.microsoft.com/kb/319401/EN-US/[^] I just substituted the ObjectCompare to an Integer Comparison and used the ListView.Index instead of the text. It works great and now I can customize my sorts to however I want. Thanks for the help Travis. -Garrett

            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