c#datagridview 排序行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1073594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:35:55  来源:igfitidea点击:

c# datagridview order rows?

c#datagridview

提问by Goober

I have a datagridview with a number of columns, one of these is a datetime column. I want to display the rows from most recent downwards. e.g. Today Yesterday The Day Before Yesterday etc.

我有一个包含多列的数据网格视图,其中之一是日期时间列。我想显示最近向下的行。例如今天昨天昨天前一天等。

Is it possible to do this with the datagridview? The gridviews datasource is an xmldocument.......

是否可以使用 datagridview 来做到这一点?gridviews 数据源是一个xmldocument......

help appreciated greatly.

帮助非常感谢。

Regards,

问候,

采纳答案by Goober

 this.dataGridView1.Sort(dataGridView1.Columns["DateTime"], ListSortDirection.Ascending);

回答by Jürgen Steinblock

What is your datasource? You have to have a datasource that supports sorting.

你的数据源是什么?你必须有一个支持排序的数据源。

e.g. a DataTable.

例如一个数据表。

If you have a List you can't sort by default. In theory you need your on class that inherits from BindingList and implements IBindingList (inheritance from BindingList is not nessacary, but makes it a bit easier).

如果您有一个列表,则默认情况下无法进行排序。从理论上讲,您需要从 BindingList 继承并实现 IBindingList 的类(从 BindingList 继承不是必需的,但使它更容易一些)。

If your BingingList is bound to the DataGridView you can sort.

如果您的 BingingList 绑定到 DataGridView,您可以排序。

回答by Jamie Ide

As far as I know, sorting is not supported for XML data sources. I think your best approach will be to first load the XmlDocument into a dataset and bind that to the grid.

据我所知,XML 数据源不支持排序。我认为您最好的方法是首先将 XmlDocument 加载到数据集并将其绑定到网格。