ListView Control - Insert Method
-
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
-
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
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).
-
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).
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
-
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
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 --
-
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 --
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