我需要通过一个字符串值的列排序DataTable或DataGridView,但是在排序ASCENDING时,在BOTTOM中使用空/空值.
DataTable不是由sql语句填充,所以没有顺序.
如果我做
DataGridView1.Sort(New RowComparer(System.ComponentModel.ListSortDirection.Ascending))
那么它抛出一个异常,说DataGridView是DataBound,这是正确的,但不帮助我,我想保留它的数据.
它是.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"