【问题标题】:Sorting DataTable string column, but with null/empty at the bottom对 DataTable 字符串列进行排序,但底部为空/空
【发布时间】:2011-06-22 16:56:23
【问题描述】:

我需要按作为字符串值的列对 DataTable 或 DataGridView 进行排序,但在排序 ASCENDING 时在 BOTTOM 具有空值/空值。

DataTable 不是由 SQL 语句填充的,因此没有排序依据。

如果我这样做

DataGridView1.Sort(New RowComparer(System.ComponentModel.ListSortDirection.Ascending))

然后它抛出一个异常,说DataGridView是DataBound,这是正确的,但对我没有帮助,我想保持它的数据绑定。

它是 .NET 2.0,这意味着没有可用的 LINQ!

【问题讨论】:

    标签: c# vb.net sorting datatable gridview-sorting


    【解决方案1】:

    在某些情况下,如果您的表中有另一个额外的列,您可以这样做:

    SELECT completed, completed IS NULL AS isnull
    FROM TABLE
    ORDER BY isnull DESC, completed DESC
    



    编辑:
    在 VB.NET 中是这样的

            For Each srSearchResult In srcSearchResultCollection
    
                Try
                    dr = dt.NewRow()
                    dr("cn") = srSearchResult.GetDirectoryEntry().Properties("cn").Value
                    dr("Account") = srSearchResult.GetDirectoryEntry().Properties("sAMAccountName").Value
                    dr("Nachname") = srSearchResult.GetDirectoryEntry().Properties("sn").Value
                    dr("Vorname") = srSearchResult.GetDirectoryEntry().Properties("givenname").Value
                    dr("Mail") = srSearchResult.GetDirectoryEntry().Properties("mail").Value
                    dr("HomeDirectory") = srSearchResult.GetDirectoryEntry().Properties("homedirectory").Value
                    dr("LogonScript") = srSearchResult.GetDirectoryEntry().Properties("scriptPath").Value
    
                    dr("IsNull") = String.IsNullOrEmpty(dr("Nachname").ToString())
    
                    dt.Rows.Add(dr)
                Catch ex As Exception
    
                End Try
    
            Next srSearchResult
    dt.DefaultView.Sort = "IsNull ASC, Nachname ASC"
    

    【讨论】:

    • @Waqas Raja:它很脏,但它确实有效!有趣的是,我似乎总是想念最简单的解决方案。
    • 酷,那么你可以将我的答案设置为答案;-)
    【解决方案2】:

    Davide Piras 有一个很好的解决方案,但是我有另一个最简单的解决方案

    添加一个新列并在一行中完成

    // just, add a new column
    ActualDataTable.Columns.Add("NullEmptyCheck", typeof(int), "ColumnNameToSort is Null OR ColumnNameToSort = ''");
    
    // apply sort expression
    ActualDataTable.DefaultView.Sort = "NullEmptyCheck asc, ColumnNameToSort asc";
    // pass datasource to grid
    MyGridView.DataSource = ActualDataTable.DefaultView;
    MyGridView.DataBind();
    

    【讨论】:

      【解决方案3】:

      即使你绑定到用户界面控件 DataGridView 的 DataTable 不是通过 SQL 填充的,你仍然可以使用 DataView 对其进行排序,你可以这样做:

      DataView myView = myDataTable.DefaultView;
      myView,Sort = "yourColumnName ASC";
      

      然后你做你的绑定。

      它是如何工作的?空值是在顶部还是底部?

      【讨论】:

      • 是的,但它不会将 Null/空值放在底部。
      • 当然,这正是我所做的,只是你没有完全意识到问题的重点是在底部获得 null/empty,这正是不会发生的事情.
      • 我已经添加了另一个答案和一个可能的解决方案,在这个答案中我只是想指出如何将排序应用于 DataTable 而不是 UI 控件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2015-09-21
      • 1970-01-01
      • 2021-03-30
      相关资源
      最近更新 更多