DataGridView 链接到 BindingList:对行进行排序不起作用

问题描述 投票:0回答:1

我是初学者。 我正在使用 Visual Studio 和 Forms 开发 C# 程序。我的表单有一个链接到 BindingList 的 DataGridView。我希望能够通过单击列标题对行进行排序。 我将 SortMode 设置为“自动”,但它不起作用:列标题中没有字形,单击它不会对行进行排序。

这是我的代码:

public class SimpleTask
{
    public string Title { get; set; }
    public string CategoryName { get; set; }
}

//Code in my Form :
BindingSource tasksBinding = new BindingSource();

//uiTaskGridView is the name of my DataGridView
uiTaskGridView.AutoGenerateColumns = false;

//add one column
var taskNameColumn = new DataGridViewTextBoxColumn
{
    DataPropertyName = "Title",
    HeaderText = "Titre",
    Width = 300,
    Resizable = DataGridViewTriState.False,
    SortMode = DataGridViewColumnSortMode.Automatic
};
uiTaskGridView.Columns.Add(taskNameColumn);

//create bindinglist and populate
var bindingList = new BindingList<SimpleTask>();
bindingList.Add(new SimpleTask { Title = "Complete project", CategoryName = "Work" });
bindingList.Add(new SimpleTask { Title = "Buy groceries", CategoryName = "Personal" });

tasksBinding.DataSource = bindingList;

uiTaskGridView.DataSource = tasksBinding;

结果是:

有人可以告诉我为什么我无法对“标题”列进行排序吗?

非常感谢。

c# sorting datagridview bindinglist
1个回答
0
投票

在stackoverflow中搜索得不够,但在问答中更深入地浏览,我找到了答案:我的数据存储在List <>中,它不支持排序。我尝试了 BindingList<> 但它也不起作用

最后,我设法找到了一个 SortableBindingList<> 实现,它确实支持排序并且工作正常!我刚刚替换了“var bindingList = new BindingList();”与“var bindingList = new SortableBindingList();”。

这是 SortableBindingList<> 的代码:

   public class SortableBindingList<T> : BindingList<T>
{
    private bool isSorted;
    private ListSortDirection sortDirection;
    private PropertyDescriptor sortProperty;

    // Constructeur sans paramètres
    public SortableBindingList() : base() { }

    // Constructeur qui accepte une liste
    public SortableBindingList(IList<T> list) : base(list) { }

    protected override bool SupportsSortingCore => true;

    protected override bool IsSortedCore => isSorted;

    protected override ListSortDirection SortDirectionCore => sortDirection;

    protected override PropertyDescriptor SortPropertyCore => sortProperty;

    protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
    {
        var items = (List<T>)Items;
        var propertyInfo = typeof(T).GetProperty(prop.Name);

        if (propertyInfo != null)
        {
            items.Sort((x, y) =>
            {
                var valueX = propertyInfo.GetValue(x);
                var valueY = propertyInfo.GetValue(y);
                return direction == ListSortDirection.Ascending
                    ? Comparer<object>.Default.Compare(valueX, valueY)
                    : Comparer<object>.Default.Compare(valueY, valueX);
            });
            isSorted = true;
            sortProperty = prop;
            sortDirection = direction;
        }
        else
        {
            isSorted = false;
        }

        // Raise the ListChanged event so bound controls refresh their data.
        OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }

    protected override void RemoveSortCore()
    {
        isSorted = false;
        sortProperty = null;
        sortDirection = ListSortDirection.Ascending;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.