WPF DataGrid/List 自定义排序和条件
好的,我在列表中有一些数据,然后将其绑定到 DataGrid。数据看起来与此类似。
Code Product Weight
008 811 10.0
008 842 12.0
I008 852 8.0
008 N/A 0.0
I008 ALL Version 0.516
VAL N/A 1.0
我如何根据以下规则对此进行排序
- 任何产品中 = N/A 的内容都必须排到顶部
- 代码中以字母开头的任何内容 我排到底部
- 任何其他内容都按代码排序
所以上面的表格就是这样的。
Code Product Weight
008 N/A 0.0
VAL N/A 1.0
008 811 10.0
008 842 12.0
I008 852 8.0
I008 ALL Version 0.516
Ok I have some data in a List which is then Bound to a DataGrid. The Data looks similar to this.
Code Product Weight
008 811 10.0
008 842 12.0
I008 852 8.0
008 N/A 0.0
I008 ALL Version 0.516
VAL N/A 1.0
How do I sort this based on the following rules
- Anything in Product that = N/A must go to the top
- Anything in Code that starts with the letter I goes to the bottom
- Anything else just sort by Code
So the above table would like this.
Code Product Weight
008 N/A 0.0
VAL N/A 1.0
008 811 10.0
008 842 12.0
I008 852 8.0
I008 ALL Version 0.516
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以纠正自己的排序算法。
通常,C# 有几个可以实现的接口,例如 IComparable 或 IEquatable,Array.Sort() 等方法使用这些接口对数组或集合进行排序。
干杯,
费利佩
You could right your own sorting algorithm.
Usually C# has several interfaces that you can implement like IComparable or IEquatable that are used by methods like Array.Sort() to sort an array or a collection.
Cheers,
Felipe
无需经历麻烦并编写自己的排序算法,我只需根据您最终想要呈现的条件预先查询数据。
如果来自 MySQL,您可以使用 IF()...如果来自 SQL-Server 等其他数据库,您可能必须替换为使用 CASE WHEN / ELSE END
Without going through hoops and writing your own sorting algorithm, I would just pre-query the data ORDERED BY the condition you ultimately want to present.
If from MySQL, you can use the IF()... If from others like SQL-Server, you may have to substitute to using CASE WHEN / ELSE END