如何对数据绑定的DataGridView列进行排序?

12

我知道这个话题有很多问题。我已经看过所有的问题了,但是似乎没有什么帮助。

如何通过点击列标题进行排序?

我应该如何修改这段代码才能完成任务?

public partial class Form1 : Form
{

    public Form1()
    {

        List<MyClass> list = new List<MyClass>();
        list.Add(new MyClass("Peter", 1202));
        list.Add(new MyClass("James", 292));
        list.Add(new MyClass("Bond", 23));

        BindingSource bs = new BindingSource();
        bs.DataSource = list;

        DataGridView dg = new DataGridView();

        DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
        c.Name = "name";
        c.DataPropertyName = "Name";
        dg.Columns.Add(c);

        c = new DataGridViewTextBoxColumn();
        c.Name = "number";
        c.DataPropertyName = "Number";
        dg.Columns.Add(c);

        dg.DataSource = bs;

        this.Controls.Add((Control)dg);

    }

}

class MyClass:IComparable<MyClass>
{
    public string Name { get; set; }
    public int Number { get; set; }

    public MyClass(){}

    public MyClass(string name,int number)
    {
        Name = name;
        Number = number;
    }

    public override string ToString()
    {
        return string.Format("{0}:{1}",Name,Number);
    }

    #region IComparable<MyClass> Members

    public int CompareTo(MyClass other)
    {
        return Name.CompareTo(other.Name);
    }

    #endregion
}
2个回答

17

我记得在我的数据网格中添加排序时遇到了一些问题,也无法找到适合的解决方案。你可以通过先添加以下类来实现可排序绑定列表。这是一个实现BindingList<T>的列表实现,因此您可以将您的数据网格绑定到它上面,并且它还支持排序。比我能给出的更好的详细说明在MSDN 这里

public class SortableBindingList<T> : BindingList<T>
{
    private ArrayList sortedList;
    private ArrayList unsortedItems;
    private bool isSortedValue;

public SortableBindingList()
{
}

public SortableBindingList(IList<T> list)
{
    foreach (object o in list)
    {
        this.Add((T)o);
    }
}

protected override bool SupportsSearchingCore
{
    get
    {
        return true;
    }
}

protected override int FindCore(PropertyDescriptor prop, object key)
{
    PropertyInfo propInfo = typeof(T).GetProperty(prop.Name);
    T item;

    if (key != null)
    {
       for (int i = 0; i < Count; ++i)
        {
            item = (T)Items[i];
            if (propInfo.GetValue(item, null).Equals(key))
                return i;
        }
    }
    return -1;
}

public int Find(string property, object key)
{
    PropertyDescriptorCollection properties =
        TypeDescriptor.GetProperties(typeof(T));
    PropertyDescriptor prop = properties.Find(property, true);

    if (prop == null)
        return -1;
    else
        return FindCore(prop, key);
}

protected override bool SupportsSortingCore
{
    get { return true; }
}


protected override bool IsSortedCore
{
    get { return isSortedValue; }
}

ListSortDirection sortDirectionValue;
PropertyDescriptor sortPropertyValue;

protected override void ApplySortCore(PropertyDescriptor prop,
    ListSortDirection direction)
{
    sortedList = new ArrayList();

   Type interfaceType = prop.PropertyType.GetInterface("IComparable");

    if (interfaceType == null && prop.PropertyType.IsValueType)
    {
        Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);

         if (underlyingType != null)
        {
            interfaceType = underlyingType.GetInterface("IComparable");
        }
    }

    if (interfaceType != null)
    {
        sortPropertyValue = prop;
        sortDirectionValue = direction;

        IEnumerable<T> query = base.Items;
        if (direction == ListSortDirection.Ascending)
        {
            query = query.OrderBy(i => prop.GetValue(i));
        }
        else
        {
            query = query.OrderByDescending(i => prop.GetValue(i));
        }
        int newIndex = 0;
        foreach (object item in query)
        {
            this.Items[newIndex] = (T)item;
            newIndex++;
        }
        isSortedValue = true;
        this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

    }
    else
    {
        throw new NotSupportedException("Cannot sort by " + prop.Name +
            ". This" + prop.PropertyType.ToString() +
            " does not implement IComparable");
    }
}

protected override void RemoveSortCore()
{
    int position;
    object temp;

    if (unsortedItems != null)
    {
        for (int i = 0; i < unsortedItems.Count; )
        {
            position = this.Find("LastName",
                unsortedItems[i].GetType().
                GetProperty("LastName").GetValue(unsortedItems[i], null));
            if (position > 0 && position != i)
            {
                temp = this[i];
                this[i] = this[position];
                this[position] = (T)temp;
                i++;
            }
            else if (position == i)
                i++;
            else
                unsortedItems.RemoveAt(i);
        }
        isSortedValue = false;
        OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }
}

public void RemoveSort()
{
    RemoveSortCore();
}
protected override PropertyDescriptor SortPropertyCore
{
    get { return sortPropertyValue; }
}

protected override ListSortDirection SortDirectionCore
{
    get { return sortDirectionValue; }
}

}

有了这个设置,你需要对你上面发布的代码做出的唯一更改就是基于你的列表创建一个SortableBindingList并绑定到可排序的列表,而不是标准的列表,像这样:

List<MyClass> list = new List<MyClass>();
list.Add(new MyClass("Peter", 1202));
list.Add(new MyClass("James", 292));
list.Add(new MyClass("Bond", 23));

// Added sortable list...
SortableBindingList<MyClass> sortableList = new SortableBindingList<MyClass>(list);

BindingSource bs = new BindingSource();
bs.DataSource = sortableList;   // Bind to the sortable list

这就足以让你开始了。


这个例子真的很棒!非常感谢! - Mike Malter

5

1
提供一个完全实现了SortableBindingList的项目链接,只需将两个文件放入你自己的项目中即可使用。请注意,作者已经在http://www.timvw.be/presenting-the-sortablebindinglistt-take-two/更新了一些代码——只需下载zip文件即可。 - Tom Bushell
对于任何看到损坏链接的人,这里是更新链接:https://timvw.be/2008/08/02/presenting-the-sortablebindinglistt-take-two/ - Jeremy Hodge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接