问题描述
我需要使用自然排序(就像在资源管理器中一样)对 DataGridView 进行排序,以便数字和文本(在同一列中)自然排序,而不是按字母顺序排序(以便位置 3"在位置 20"之前,等等.)我有一个 DataGridView,我在其中将 DataView 设置为 DataSource.DataView 包含一个 DataTable,它是用数据库中的一些值创建的.列类型是字符串.我有一个 IComparer,它可以做它应该做的事情,但我不知道如何使用它,因为我无法找到如何进行排序.DataGridView.SortCompare 事件将是完美,它不起作用,因为它是数据绑定的.DataView.Sort,只接受带有列名和排序顺序的字符串.
I need to sort a DataGridView with Natural Sorting (Like in Explorer) so that numbers and text (in the same column) are sorted naturally, and not alphabetically (so that "place 3" comes before "place 20", etc.). I have a DataGridView, where I have set a DataView as DataSource. The DataView contains a DataTable which is created with some values from a database. The column types are string. I have an IComparer, which does what it should, but I can't figure out how to use it, cause I can't find out how to do the sorting. The DataGridView.SortCompare event, which would be perfect, doesn't work since it is databound. The DataView.Sort, only accept strings with column names and sort orders.
很烦人.试图在 StackOverflow 上阅读相关问题,并在谷歌上搜索了很多,但我真的找不到太多关于这个的信息.我真正找到的唯一东西是使用数据视图的 Sort(string) 方法,该方法不起作用,因为它按字母顺序排序.
Very annoying. Tried to read related issues here on StackOverflow, and searched google lots and lots, but I can't really find much about this. Only stuff I really find is using that Sort(string) method of the dataview, which wont work, since it sorts alphabetically.
有谁知道如何在没有太多麻烦的情况下做到这一点?一定是除了我之外的其他人在为此苦苦挣扎吗?我真的不想重新实现整个 datagridview 或 dataview 类,只是为了获得自定义排序...
Does anyone know how to do this without too much trouble? It got to be others than me struggeling with this? I really don't want to re-implement the whole datagridview or dataview classes, just to get custom sorting...
更新:如果有人想知道,我仍在寻找这个问题的好答案.虽然与此同时,我最终创建了自己的简单表类,然后手动将其输入到 datagridview 中.覆盖 SortCompare 方法.有点烦人,但并不太难,因为我只需要显示值(无需编辑或任何内容),因此可以将所有内容转换为字符串.
Update: In case someone were wondering, I'm still looking for a good answer to this problem. Although in the mean time, I ended up creating my own simple table class, and then feed that into a datagridview manually. Overriding the SortCompare method. Bit annoying, but wasn't too hard, since I only need to show values (no editing or anything) and therefore could convert everything to strings.
推荐答案
看一看 此 MSDN 页面 和 这篇博文.原则上需要在数据源(无论是ObjectDataSource还是SqlDataSource)配置排序,而不是GridView.
Take a look at this MSDN page and this blog post. In principle, you need to configure the sorting at the data source (whether its an ObjectDataSource or a SqlDataSource) not at the GridView.
据我所知,除了简单的升序/降序排序之外,DataView 类不支持任何其他内容.如果没有看到加载和绑定数据的代码,就很难提出具体建议,但您可以:
As far as I can tell the DataView class doesn't support anything other than a simple ascending/decending sort. Without seeing the code where you load and bind the data it's hard to make a specific recommendation, but you could either:
- 将您的数据加载到 List 而不是 DataTable 中,调用传入比较方法的 Sort 方法,然后绑定到该列表.
- 在您的 aspx 代码中创建一个直接从类中获取数据的 ObjectDataSource,并将该 ObjectDataSource 配置为使用您的 IComparer.
这篇关于C#:自定义排序的 DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!