我需要按作为字符串值的列对 DataTable 或 DataGridView 进行排序,但在按升序排序时底部为空/空值。
DataTable 不是由 SQL 语句填充的,因此没有排序依据。
如果我这样做
DataGridView1.Sort(New RowComparer(System.ComponentModel.ListSortDirection.Ascending))
然后它抛出一个异常,说 DataGridView 是 DataBound,这是正确的,但对我没有帮助,我想保持它的数据绑定(bind)。
它是 .NET 2.0,这意味着没有可用的 LINQ!
最佳答案
在某些情况下,如果您的表中有另一个额外的列,您可以这样做:
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"
关于c# - 对 DataTable 字符串列进行排序,但底部为空/空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4921054/