排序列后无法添加列表视图项 [英] Unable to add listview items after sorting colum

查看:67
本文介绍了排序列后无法添加列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用于向listview添加项目的代码。



Below is the code i am using for adding items to 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专栏的类





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,
                                   ((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获取以下错误消息。任何帮助都会很有用。





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 />





我尝试过:



i已经浏览了所有谷歌页面,但找不到任何完美的解决方案。



What I have tried:

i have gone through all google pages but couldn't find any perfect solution.

推荐答案

终于找到了解决方案。但可怜的部分是我不确定它是如何工作的......



在Listview项目删除部分之前添加以下行解决问题



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;





PS:可能在重新创建listview项目之前我已将排序设置为NONE以解决我的问题。



P.S: May be before recreating listview items i settled the sorting in to NONE which resolves my issue.


这篇关于排序列后无法添加列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆