C#Winforms Listview列最初排序,但不打开第二次单击

时间:2017-05-19 13:53:53

标签: c# winforms sorting listview

范围:按ColumnClick升序和/或降序排序列表视图。

列单击正在运行并对列Descending进行排序,但是单击同一列或任何列不会对Ascending进行排序。无法弄清楚我错过了什么。

ColumnClick事件

        // The column we are currently using for sorting.
    private ColumnHeader SortingColumn = null;

    private void list_ProjectsColumnClick(object sender, ColumnClickEventArgs e)
    {
        // Get the new sorting column.
        ColumnHeader new_sorting_column = listProjects.Columns[e.Column];

        // Figure out the new sorting order.
        System.Windows.Forms.SortOrder sort_order;
        if (SortingColumn == null)
        {
            // New column. Sort ascending.
            sort_order = System.Windows.Forms.SortOrder.Ascending;
        }
        else
        {
            // See if this is the same column.
            if (new_sorting_column == SortingColumn)
            {
                // Same column. Switch the sort order.
                if (SortingColumn.Text.StartsWith("> "))
                {
                    sort_order = System.Windows.Forms.SortOrder.Descending;
                }
                else
                {
                    sort_order = System.Windows.Forms.SortOrder.Ascending;
                }
            }
            else
            {
                // New column. Sort ascending.
                sort_order = System.Windows.Forms.SortOrder.Ascending;
            }

            // Remove the old sort indicator.
            SortingColumn.Text = SortingColumn.Text.Substring(2);
        }

        // Display the new sort order.
        SortingColumn = new_sorting_column;
        if (sort_order == System.Windows.Forms.SortOrder.Ascending)
        {
            SortingColumn.Text = "> " + SortingColumn.Text;
        }
        else
        {
            SortingColumn.Text = "< " + SortingColumn.Text;
        }

        // Create a comparer.
        listProjects.ListViewItemSorter =
            new ListViewComparer(e.Column, sort_order);

        // Sort.
        listProjects.Sort();
    }

ListViewComparer类

    class ListViewComparer : IComparer
    {
    private int ColumnNumber;
    private System.Windows.Forms.SortOrder SortOrder;

    public ListViewComparer(int column_number,
        System.Windows.Forms.SortOrder sort_order)
    {
        ColumnNumber = column_number;
        SortOrder = sort_order;
    }

    // Compare two ListViewItems.
    public int Compare(object object_x, object object_y)
    {
        // Get the objects as ListViewItems.
        ListViewItem item_x = object_x as ListViewItem;
        ListViewItem item_y = object_y as ListViewItem;

        // Get the corresponding sub-item values.
        string string_x;
        if (item_x.SubItems.Count <= ColumnNumber)
        {
            string_x = "";
        }
        else
        {
            string_x = item_x.SubItems[ColumnNumber].Text;
        }

        string string_y;
        if (item_y.SubItems.Count <= ColumnNumber)
        {
            string_y = "";
        }
        else
        {
            string_y = item_y.SubItems[ColumnNumber].Text;
        }

        // Compare them.
        int result;
        double double_x, double_y;
        if (double.TryParse(string_x, out double_x) &&
            double.TryParse(string_y, out double_y))
        {
            // Treat as a number.
            result = double_x.CompareTo(double_y);
        }
        else
        {
            DateTime date_x, date_y;
            if (DateTime.TryParse(string_x, out date_x) &&
                DateTime.TryParse(string_y, out date_y))
            {
                // Treat as a date.
                result = date_x.CompareTo(date_y);
            }
            else
            {
                // Treat as a string.
                result = string_x.CompareTo(string_y);
            }
        }

        // Return the correct result depending on whether
        // we're sorting ascending or descending.
        if (SortOrder == System.Windows.Forms.SortOrder.Ascending)
        {
            return result;
        }
        else
        {
            return -result;
        }
    }

0 个答案:

没有答案