कॉलम सॉर्ट करने के बाद सूचीदृश्य आइटम जोड़ने में असमर्थ


नीचे वह कोड है जिसका उपयोग मैं सूचीदृश्य में आइटम जोड़ने के लिए कर रहा हूं।

सी#
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;
                           //}


                       }
                   }

नीचे वह कक्षा है जिसका उपयोग मैं सूचीदृश्य कॉलम को सॉर्ट करने के लिए कर रहा हूं

सी#
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;
       }
   }

इस सूचीदृश्य सॉर्टिंग कॉलम को शामिल करने के बाद। यदि मैं कॉलमों को क्रमबद्ध करने का प्रयास करता हूं तो सूचीदृश्य में आइटम जोड़ने के लिए फ़ाइल खोलें और नीचे त्रुटि संदेश प्राप्त हो रहा है। किसी भी मदद की बहुत सराहना होगी.

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

मैंने क्या प्रयास किया है:

मैंने सभी गूगल पेज देखे लेकिन कोई सटीक समाधान नहीं मिला।

समाधान 1

आख़िरकार मुझे स्वयं ही समाधान मिल गया। लेकिन ख़राब बात यह है कि मुझे यकीन नहीं है कि यह कैसे काम करता है…

सूचीदृश्य आइटम के भाग को हटाने से पहले नीचे दी गई पंक्ति जोड़ी गई जिससे समस्या हल हो गई

सी#
listView1.Sorting = System.Windows.Forms.SortOrder.None;

पुनश्च: हो सकता है कि सूचीदृश्य आइटमों को दोबारा बनाने से पहले मैंने छँटाई को NONE में व्यवस्थित कर दिया हो जिससे मेरी समस्या का समाधान हो गया हो।

समाधान 2

ListView1.ListViewItemSorter = कुछ नहीं

コメント

タイトルとURLをコピーしました