English 中文(简体)
How to sort databound DataGridView column?
原标题:

I know that there are a lot of questions on this topic. I have been through all of them but nothing seems to help.

How to sort by clicking on column header?

How should I modify this code to do the job?

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
}
最佳回答

I recall having issues finding something that would work when I added sorting to my datagrids too. You can implement a sortable bindable list by first adding the following class to your project. It is a list implementation that implements BindingList<T>, so that you can bind your datagrid to it, and it also supports sorting. A better explanation of the details than I could give is on MSDN here

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; }
}

}

With that in place, the only changes you need to make to the code that you have posted above is to create a SortableBindingList based on your list and bind to the sortable list, rather than the standard one, like so:

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

And that will be enough to get you going.

问题回答

Here is the blog post that really really helped me.

Presenting the SortableBindableList

Also, check out How do I implement automatic sorting of DataGridView? which has examples of this and another library.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签