C#:自定义类型的DataGridView [英] C#: Custom sort of DataGridView

查看:619
本文介绍了C#:自定义类型的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用自然排序来排序DataGridView(像在Explorer中),以便数字和文本(在同一列中)自然排序,而不是按字母顺序排列(这样place 3来到place 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上阅读相关问题,并搜索了google很多很多,但是我不太清楚这一点。只有我真正发现的东西是使用dataview的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...

更新:In有人在想,我还在寻找一个很好的答案这个问题。虽然在同一时间,我最终创建了我自己的简单的表类,然后手动将它输入到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:


  1. 将数据加载到一个List而不是一个DataTable,调用Sort方法传递比较方法,然后绑定到该列表。

  2. 在你的aspx代码中创建一个ObjectDataSource,从一个类直接获取数据,并配置ObjectDataSource使用您的IComparer。

这篇关于C#:自定义类型的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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