简体   繁体   English

C#-排序datagridview的麻烦

[英]C# - troubles with sorting datagridview

I have searched my ass off for the answer of my problem. 我已经搜寻了我的屁股来解决我的问题。 I have developed ac# Winforms program with a few datagridviews. 我已经开发了带有一些datagridviews的ac#Winforms程序。

The problem is that I want to be the user able to sort the datagridview by clicking on the columnheader (i thought this would be standard...) But it is just not working. 问题是我希望成为能够通过单击列标题对datagridview进行排序的用户(我认为这将是标准的……),但是它不起作用。

I tried the dgv.Sort-method but this throws an exception that the datagridview has to be bound to an IBindingList but I have no clue how to do that and I really do not want to redevelope everything.. 我尝试了dgv.Sort方法,但这引发了一个例外,即必须将datagridview绑定到IBindingList,但是我不知道如何执行该操作,并且我真的不想重新开发所有内容。

Here's how I populate my dgv. 这是我填充dgv的方法。

I have certain custom objects and put them into a list. 我有某些自定义对象,并将它们放入列表中。 When this list is fullypopulated I set it as the datasource for the dgv. 当此列表完全填充后,我将其设置为dgv的数据源。

list.Add(costumobject);
.
.
.
dgv.DataSource = list;

Could you please tell me a quick way to make the sort-function working? 您能告诉我一种使排序功能起作用的快速方法吗?

Kind regards, 亲切的问候,

List<T> doesn't support sorting directly. List<T>不支持直接排序。

Instead you can use a Linq routine to do the sorting. 相反,您可以使用Linq例程进行排序。

However you will need to include a sort field check, which will be as long as your number of columns.. 但是,您将需要包括一个排序字段检查,该检查与列数一样长。

Not knowing your customobject class let's try with a Name class: 不知道您的customobject类,让我们尝试使用Name类:

class Name
{
    public string first { get; set; }
    public string last { get; set; }
    public string middle { get; set; }

    public Name (string f, string m, string l)
    {
        first = f; middle = m; last = l;
    }
}

Now let's code the ColumnHeaderMouseClick event: 现在,让我们对ColumnHeaderMouseClick事件进行编码:

private void dataGridView1_ColumnHeaderMouseClick(object sender, 
                           DataGridViewCellMouseEventArgs e)
{
    List<Name> names = dataGridView1.DataSource as List<Name>;
    string col = dataGridView2.Columns[e.ColumnIndex].DataPropertyName;
    string order =  " ASC";
    if (dataGridView1.Tag != null) 
        order = dataGridView1.Tag.ToString().Contains(" ASC") ? " DESC" : " ASC";

    dataGridView1.Tag = col + order;

    if (order.Contains(" ASC"))
    names = names.OrderBy(x => col == "first"? x.first 
                             : col == "last" ? x.last : x.middle).ToList();  
    else
    names = names.OrderByDescending(x => col == "first"? x.first : 
                                         col == "last" ? x.last : x.middle).ToList();  

    dataGridView1.DataSource = names;
}

Note that I store the current sort column and order in the DGV's Tag . 请注意,我将当前的排序列和顺序存储在DGV的Tag You can move it to a class level variable or some other place. 您可以将其移动到类级别的变量或其他位置。 Unfortunately the DGV's SortOrder property can't be set.. 不幸的是,无法设置DGV的SortOrder属性。

Create a SortableBindingList instead of a List. 创建一个SortableBindingList而不是一个List。

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace YourNamespace
{
    /// <summary>
    /// Provides a generic collection that supports data binding and additionally supports sorting.
    /// See http://msdn.microsoft.com/en-us/library/ms993236.aspx
    /// If the elements are IComparable it uses that; otherwise compares the ToString()
    /// </summary>
    /// <typeparam name="T">The type of elements in the list.</typeparam>
    public class SortableBindingList<T> : BindingList<T> where T : class
    {
        private bool _isSorted;
        private ListSortDirection _sortDirection = ListSortDirection.Ascending;
        private PropertyDescriptor _sortProperty;

        /// <summary>
        /// Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
        /// </summary>
        public SortableBindingList()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
        /// </summary>
        /// <param name="list">An <see cref="T:System.Collections.Generic.IList`1" /> of items to be contained in the <see cref="T:System.ComponentModel.BindingList`1" />.</param>
        public SortableBindingList(IList<T> list) : base(list)
        {
        }

        /// <summary>
        /// Gets a value indicating whether the list supports sorting.
        /// </summary>
        protected override bool SupportsSortingCore
        {
            get { return true; }
        }

        /// <summary>
        /// Gets a value indicating whether the list is sorted.
        /// </summary>
        protected override bool IsSortedCore
        {
            get { return _isSorted; }
        }

        /// <summary>
        /// Gets the direction the list is sorted.
        /// </summary>
        protected override ListSortDirection SortDirectionCore
        {
            get { return _sortDirection; }
        }

        /// <summary>
        /// Gets the property descriptor that is used for sorting the list if sorting is implemented in a derived class; otherwise, returns null
        /// </summary>
        protected override PropertyDescriptor SortPropertyCore
        {
            get { return _sortProperty; }
        }

        /// <summary>
        /// Removes any sort applied with ApplySortCore if sorting is implemented
        /// </summary>
        protected override void RemoveSortCore()
        {
            _sortDirection = ListSortDirection.Ascending;
            _sortProperty = null;
            _isSorted = false; //thanks Luca
        }

        /// <summary>
        /// Sorts the items if overridden in a derived class
        /// </summary>
        /// <param name="prop"></param>
        /// <param name="direction"></param>
        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            _sortProperty = prop;
            _sortDirection = direction;

            List<T> list = Items as List<T>;
            if (list == null) return;
            list.Sort(Compare);
            _isSorted = true;
            //fire an event that the list has been changed.
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        private int Compare(T lhs, T rhs)
        {
            var result = OnComparison(lhs, rhs);
            //invert if descending
            if (_sortDirection == ListSortDirection.Descending)
                result = -result;
            return result;
        }

        private int OnComparison(T lhs, T rhs)
        {
            object lhsValue = lhs == null ? null : _sortProperty.GetValue(lhs);
            object rhsValue = rhs == null ? null : _sortProperty.GetValue(rhs);
            if (lhsValue == null)
            {
                return (rhsValue == null) ? 0 : -1; //nulls are equal
            }

            if (rhsValue == null)
            {
                return 1; //first has value, second doesn't
            }

            if (lhsValue is IComparable)
            {
                return ((IComparable)lhsValue).CompareTo(rhsValue);
            }

            if (lhsValue.Equals(rhsValue))
            {
                return 0; //both are the same
            }

            //not comparable, compare ToString
            return lhsValue.ToString().CompareTo(rhsValue.ToString());
        }
    }
}

have you tried to refresh the datagrid after changing datasource? 更改数据源后您是否尝试过刷新数据网格?

list.Add(costumobject);
.
.
.
dgv.DataSource = list;
dgv.Refresh();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM