如何实现DataGridView中的自动分拣? [英] How do I implement automatic sorting of DataGridView?

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

问题描述

我编程添加列到一个DataGridView然后结合到列表。缺省情况下,各列的SortMode是自动。但是,当我跑我的应用程序,点击标题什么都不做。向上/向下箭头没有显示出来。从阅读MSDN,不多说有关自动排序。他们进入有关程序排序的详细细节。所以,我假设自动方式应该很容易。 MSDN接着说:除非列标头用于选择后,单击列标题自动排序该列在DataGridView并显示字形指出排序顺序。这到底是什么意思呢?难道我要设置与排序相冲突的格属性?我缺少什么?

 的AutoGenerateColumns =假;
AllowUserToAddRows = FALSE;
AllowUserToDeleteRows = FALSE;
AllowUserToResizeRows = FALSE;
AllowUserToResizeColumns = FALSE;
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
只读= TRUE;
多选= FALSE;
RowHeadersVisible = FALSE;
的SelectionMode = DataGridViewSelectionMode.FullRowSelect;
CellBorderStyle = DataGridViewCellBorderStyle.None;
    DataGridViewTextBoxColumn idColumn =新DataGridViewTextBoxColumn();
    idColumn.HeaderText =ID;
    idColumn.DataPropertyName =IDNumber中;    DataGridViewTextBoxColumn名称列=新DataGridViewTextBoxColumn();
    nameColumn.HeaderText =姓名;
    nameColumn.DataPropertyName =说明;    DataGridViewTextBoxColumn lastModifiedColumn =新DataGridViewTextBoxColumn();
    lastModifiedColumn.HeaderText =最后修改;
    lastModifiedColumn.DataPropertyName =日期;    Columns.Add(idColumn);
    Columns.Add(名称列);
    Columns.Add(lastModifiedColumn);    清单< IMyObject>的BindingList = GetMyList();
    数据源=的BindingList;


解决方案

我们使用 BindingListView 绑定列表< T> s到DataGridViews ,它的美丽为我们工作。


  

下面是创建对象的列表的视图(C#)的一个很简单的例子:


 列表<客户>客户= GetCustomers的();
BindingListView<客户>鉴于=新BindingListView<客户>(客户);
dataGridView1.DataSource =视图。

查看 http://stackoverflow.com/a/17952576/116891 了解了很多关于DGV排序的更多细节和数据绑定。

如果您不希望补充一点,就是沉重的,你可以试试这个实施 SortableBindingList&LT的; T> 与更新

这两个给你整理的权利开箱,并BindingListView甚至比DataViews的更快,根据自己的基准。

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;

解决方案

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

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;

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

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

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

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

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