Невозможно добавить элементы listview после сортировки colum
Ниже приведен код, который я использую для добавления элементов в listview.
OpenFileDialog FLoc = new OpenFileDialog(); Application.DoEvents(); FLoc.Title = "Select Host Name File"; FLoc.Filter = "TXT files|*.txt"; FLoc.InitialDirectory = @"C:\"; if (FLoc.ShowDialog() == DialogResult.OK) { try { FileName = FLoc.FileName; FileLines = File.ReadAllLines(FileName); if (listView1.Items.Count > 0) { foreach (ListViewItem itemR in listView1.Items) { itemR.Checked = false; itemR.Remove(); } int LC = 0; foreach (var item1 in FileLines) { this.listView1.Focus(); LC = listView1.Items.IndexOf(listView1.Items.Add(item1)); // listView1.Items[LC].SubItems.Add(""); // listView1.Items[LC].Checked = true; } comboBox1.ResetText(); DTP.Value = DateTime.Today; } else { 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; //} } }
ниже приведен класс, который я использую для сортировки столбца listview
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, ((ListViewItem)y).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; } }
после включения этого столбца сортировки listview. если я попытаюсь отсортировать столбцы, то открою файл, чтобы добавить элементы в listview, получив следующее сообщение об ошибке. любая помощь была бы очень оценена.
<br /> InvalidArgument=Value of '3' is not valid for 'index'. Parameter name: index<br />
Что я уже пробовал:
я просмотрел все страницы google, но не смог найти ни одного идеального решения.
GKP1992
Что вы подразумеваете под идеальным решением?
Member 13339377
я не уверен, что именно вызывает эту проблему.