错误提示:InvalidArgument=“1”的值对于“index”无效。 感觉程序上做的没有错希望有这方面经验的朋友们,进来指点一下,谢谢了~本人在线等,问题解决就给分。

解决方案 »

  1.   

    就是照搬的MSDN里面,首先是添加列的值: for (int ii = 0; ii < listView2.Items.Count; ii++)
                            {
                                listView2.Items[ii].SubItems.Add(File.GetCreationTime(listView2.Items[ii].Text).ToString());
                            }。
    再来就是MSDN的了。
     private int sortColumn = -1; this.listView2.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView2_ColumnClick);private void listView2_ColumnClick(object sender, ColumnClickEventArgs e)
            {
                // Determine whether the column is the same as the last column clicked.
                if (e.Column != sortColumn)
                {
                    // Set the sort column to the new column.
                    sortColumn = e.Column;
                    // Set the sort order to ascending by default.
                    listView1.Sorting = SortOrder.Ascending;
                }
                else
                {
                    // Determine what the last sort order was and change it.
                    if (listView1.Sorting == SortOrder.Ascending)
                        listView1.Sorting = SortOrder.Descending;
                    else
                        listView1.Sorting = SortOrder.Ascending;
                }
                // Call the sort method to manually sort.
                listView1.Sort();
                // Set the ListViewItemSorter property to a new ListViewItemComparer
                // object.
                this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column,
                                                                  listView1.Sorting);
            }        class ListViewItemComparer : IComparer
            {
                private int col;
                private SortOrder order;
                public ListViewItemComparer()
                {
                    col = 0;
                    order = SortOrder.Ascending;
                }
                public ListViewItemComparer(int column, SortOrder order)
                {
                    col = column;
                    this.order = order;
                }
                public int Compare(object x, object y)
                {
                    int returnVal;
                    // Determine whether the type being compared is a date type.
                    //try
                    //{
                        // Parse the two objects passed as a parameter as a DateTime.
                        System.DateTime firstDate =
                                DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
                        System.DateTime secondDate =
                                DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
                        // Compare the two dates.
                        returnVal = DateTime.Compare(firstDate, secondDate);
                    //}
                    // If neither compared object has a valid date format, compare
                    // as a string.
                    //catch
                    //{
                        // Compare the two items as a string.
                        returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                                    ((ListViewItem)y).SubItems[col].Text);
                    //}
                    // Determine whether the sort order is descending.
                    if (order == SortOrder.Descending)
                        // Invert the value returned by String.Compare.
                        returnVal *= -1;
                    return returnVal;
                }
            }
      

  2.   

    可能是因为你单击的列的元素不是DateTime的格式的文本.
    我看你的代码中,好像只有SubItem是DateTime格式的文本,而Item.Text可能就不是了.如果你点的是这一列可能就出错了.