如何实现DataGridView的自动排序? [英] How do I implement automatic sorting of DataGridView?

查看:25
本文介绍了如何实现DataGridView的自动排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式将列添加到 DataGridView,然后绑定到列表.默认情况下,列的 SortMode 为 Automatic.但是当我运行我的应用程序时,点击标题没有任何作用.向上/向下箭头未显示.阅读MSDN,关于自动排序的介绍不多.他们更详细地介绍了程序化排序.所以,我假设自动方式应该很容易.MSDN 继续说除非使用列标题进行选择,否则单击列标题会自动按此列对 DataGridView 进行排序,并显示指示排序顺序的字形."这到底是什么意思呢?我可以设置与排序冲突的网格属性吗?我错过了什么?

I am programmatically adding columns to a DataGridView and then binding to a list. By default, the SortMode of the columns are Automatic. But when I run my app, clicking on the headers does nothing. The up/down arrows aren't showing up. From reading MSDN, not much is said about automatic sorting. They go into more detail about programmatic sorting. So, I'm assuming the automatic way should be easy. MSDN goes on to say "Unless column headers are used for selection, clicking the column header automatically sorts the DataGridView by this column and displays a glyph indicating the sort order." What exactly does that mean? Could I be setting a grid property that conflicts with the sorting? What am I missing?

AutoGenerateColumns = false;
AllowUserToAddRows = false;
AllowUserToDeleteRows = false;
AllowUserToResizeRows = false;
AllowUserToResizeColumns = false;
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
ReadOnly = true;
MultiSelect = false;
RowHeadersVisible = false;
SelectionMode = DataGridViewSelectionMode.FullRowSelect;
CellBorderStyle = DataGridViewCellBorderStyle.None;


    DataGridViewTextBoxColumn idColumn = new DataGridViewTextBoxColumn();
    idColumn.HeaderText = "ID";
    idColumn.DataPropertyName = "IDNumber";

    DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
    nameColumn.HeaderText = "Name";
    nameColumn.DataPropertyName = "Description";

    DataGridViewTextBoxColumn lastModifiedColumn = new DataGridViewTextBoxColumn();
    lastModifiedColumn.HeaderText = "Last Modified";
    lastModifiedColumn.DataPropertyName = "Date";

    Columns.Add(idColumn);
    Columns.Add(nameColumn);
    Columns.Add(lastModifiedColumn);

    List<IMyObject> bindingList = GetMyList();
    DataSource = bindingList;

推荐答案

我们使用BindingListView来绑定List

We use BindingListView to bind List<T>s to DataGridViews, and it's worked beautifully for us.

这是创建对象列表视图的一个非常简单的示例(在 C# 中):

Here is a very simple example of creating a view of a list of objects (in C#):

List<Customer> customers = GetCustomers();
BindingListView<Customer> view = new BindingListView<Customer>(customers);
dataGridView1.DataSource = view;

查看 https://stackoverflow.com/a/17952576/116891,了解更多有关 DGV 排序的详细信息和数据绑定.

Check out https://stackoverflow.com/a/17952576/116891 for a lot more details about DGV sorting and databinding.

如果你不想添加那么重的东西,你可以试试这个的实现SortableBindingList(有更新).

If you don't want to add something that heavy, you can try this implementation of a SortableBindingList<T> (with updates).

根据它们的基准,两者都可以让您立即进行排序,而 BindingListView 甚至比 DataViews 还要快.

Both give you sorting right out of the box, and BindingListView is even faster than DataViews, according to their benchmarks.

这篇关于如何实现DataGridView的自动排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆