WPF DataGrid/List 自定义排序和条件

发布于 2024-12-15 03:17:29 字数 605 浏览 5 评论 0原文

好的,我在列表中有一些数据,然后将其绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-12-22 03:17:29

您可以纠正自己的排序算法。

通常,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

浅沫记忆 2024-12-22 03:17:29

无需经历麻烦并编写自己的排序算法,我只需根据您最终想要呈现的条件预先查询数据。

如果来自 MySQL,您可以使用 IF()...如果来自 SQL-Server 等其他数据库,您可能必须替换为使用 CASE WHEN / ELSE END

SELECT 
      pr.Code,
      pr.Product,
      pr.Weight
   from
      ProductTable pr
   order by
      if( pr.Product = 'N/A', 1, 2 ),
      if( LEFT( pr.Code, 1 ) = 'I', 2, 1 ),
      pr.Code

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

SELECT 
      pr.Code,
      pr.Product,
      pr.Weight
   from
      ProductTable pr
   order by
      if( pr.Product = 'N/A', 1, 2 ),
      if( LEFT( pr.Code, 1 ) = 'I', 2, 1 ),
      pr.Code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文