c# - WinForms:DataGridView - 编程排序

标签 c# winforms data-binding sorting datagridview

我有一个带有 datagridview 的表单。 dataGridView 绑定(bind)到 BindingSource:

public class Address
{
    public string State { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
}
this.addressBindingSource.DataSource = typeof(Address);
this.dataGridView1.DataSource = this.addressBindingSource;

我这样填充 DataSource:

    addressBindingSource.DataSource = new BindingList<Address>
    {
        new Address {State = "S1", City = "C1", Street = "S1"},
        new Address {State = "S1", City = "C1", Street = "S2"},
        new Address {State = "S1", City = "C1", Street = "S3"},
        new Address {State = "S1", City = "C2", Street = "S4"},
        new Address {State = "S1", City = "C2", Street = "S5"},
        new Address {State = "S1", City = "C2", Street = "S6"},
    };

我正在尝试为此数据 GridView 启用排序。我将 dataGridView1 的所有列的 SortMode 设置为 Programmatic。我为 ColumnHeaderMouseClick 添加了一个事件处理程序:

    private Dictionary<int, string> columnIndexPropertyNameDictionary = new Dictionary<int, string>
        {
            {0, "State"},
            {1, "City"},
            {2, "Street"},
        };

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex < 0)
        return;

    for (int i = 0; i < dataGridView1.Columns.Count; i++)
    {
        if (i == e.ColumnIndex)
            continue;
        dataGridView1.Columns[i].HeaderCell.SortGlyphDirection = SortOrder.None;
    }

    var column = dataGridView1.Columns[e.ColumnIndex];

    if (column.SortMode != DataGridViewColumnSortMode.Programmatic)
        return;

    var sortGlyphDirection = column.HeaderCell.SortGlyphDirection;

    switch (sortGlyphDirection)
    {
        case SortOrder.None:
        case SortOrder.Ascending:
            addressBindingSource.Sort = columnIndexPropertyNameDictionary[e.ColumnIndex] + " ASC";
            column.HeaderCell.SortGlyphDirection = SortOrder.Descending;
            break;
        case SortOrder.Descending:
            addressBindingSource.Sort = columnIndexPropertyNameDictionary[e.ColumnIndex] + " DESC";
            column.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            break;
    }
}

排序仍然无效。我做错了什么?

最佳答案

问题是开箱即用的 BindingList 不支持排序!我知道 - 听起来很蠢,但事实就是如此。

您需要实现自己的 SortableBindingList。下面是一个代码示例。

此代码来自here我没有时间彻底检查它。如果它不起作用,请用谷歌搜索术语 SortableBindingList,那里有很多的实现。

public class SortableBindingList<t> : BindingList<t>
{
    private bool m_Sorted = false;
    private ListSortDirection m_SortDirection = ListSortDirection.Ascending;
    private PropertyDescriptor m_SortProperty = null;

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

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

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

    protected override PropertyDescriptor SortPropertyCore
    {
        get
        {
            return m_SortProperty;
        }
    }

    protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
    {
        m_SortDirection = direction;
        m_SortProperty = prop;
        var listRef = this.Items as List<t>;
        if (listRef == null)
            return;
        var comparer = new SortComparer<t>(prop, direction);

        listRef.Sort(comparer);

        OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
   }
}

关于c# - WinForms:DataGridView - 编程排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5700782/

相关文章:

c# - 以编程方式将图像放入不活动的表单中?

c# - 使用 Socket 从机器接收数据

c# - 在C#应用程序中,DB Connection应该创建一次,还是每次执行SQL语句时创建一次?

c# - 当 WPF 窗口发生变化时如何得到通知?

java - Spring Webflow 中的数据绑定(bind)问题

c# - 在 C# 中检索 Visio 形状的连接点名称

c# - !=-运算符对结构失败

c# - BindingSource,设置数据源,然后设置一个新的数据源

mysql - 如何从数据库中获取两个日期和时间之间的记录?

c# - 读取XML后,更改颜色不会影响属性更改