Click here to Skip to main content
14,878,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is the code i am using for adding items to listview.

OpenFileDialog FLoc = new OpenFileDialog();
           FLoc.Title = "Select Host Name File";
           FLoc.Filter = "TXT files|*.txt";
           FLoc.InitialDirectory = @"C:\";

           if (FLoc.ShowDialog() == DialogResult.OK)
                   FileName = FLoc.FileName;
                   FileLines = File.ReadAllLines(FileName);

                   if (listView1.Items.Count > 0)
                       foreach (ListViewItem itemR in listView1.Items)
                           itemR.Checked = false;

                       int LC = 0;
                       foreach (var item1 in FileLines)
                           LC = listView1.Items.IndexOf(listView1.Items.Add(item1));
                        //   listView1.Items[LC].SubItems.Add("");
                         //  listView1.Items[LC].Checked = true;

                       DTP.Value = DateTime.Today;


                       foreach (var item2 in FileLines)
                           //below condition to avoid duplicate entries in listview
                           //if (listView1.FindItemWithText(item) == null)
                           int LC = listView1.Items.IndexOf(listView1.Items.Add(item2));
                           listView1.Items[LC].Checked = true;


below is the class i am using for sorting listview column

class ListViewItemComparer : IComparer
       private int col;
       private System.Windows.Forms.SortOrder order;
       public ListViewItemComparer()
           col = 0;
           order = System.Windows.Forms.SortOrder.Ascending;
       public ListViewItemComparer(int column, System.Windows.Forms.SortOrder order)
           col = column;
           this.order = order;
       public int Compare(object x, object y)
           int returnVal = -1;
           returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
           // Determine whether the sort order is descending.
           if (order == System.Windows.Forms.SortOrder.Descending)
               // Invert the value returned by String.Compare.
               returnVal *= -1;
           return returnVal;

after including this listview sorting column. if i try to sort columns then open the file to add items to listview getting a below error message. any help would much appreicated.

<br />
InvalidArgument=Value of '3' is not valid for 'index'. Parameter name: index<br />

What I have tried:

i have gone through all google pages but couldn't find any perfect solution.
Updated 4-Oct-17 3:48am
GKP1992 4-Oct-17 8:18am
What do you mean by a perfect solution?
Member 13339377 4-Oct-17 8:39am
i am not sure what exactly causes this issue.

1 solution

finally found solution on myself. But the poor part is i am not sure how it works...

added the below line before the Listview Items remove part which resolved the issue

listView1.Sorting = System.Windows.Forms.SortOrder.None;

P.S: May be before recreating listview items i settled the sorting in to NONE which resolves my issue.
Richard MacCutchan 4-Oct-17 10:08am
When you add items to a ListView that has SortOrder = Ascending or Descending, then it will try to sort each time you add an item. So as you capture the index value of each added item, the actual item's position may change. I found your code pretty convoluted to be honest, and I think perhaps it is over complicated. You should ignore the returned index values as you add items, and just do your sorting when all items have been added.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900