我试图对数据视图进行排序,但从9,000到10,000,有些东西不起作用。10,000是放在9,000人之前的。这是我的代码和我的程序的截图

DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)发布于 2017-02-22 22:19:03
尝试使用Linq的OrderBy子句。它允许您拆分字符串并按数字进行排序。
Dim dList As List(Of Datagridviewrow) = datagridview1.Rows.Cast(of DataGridViewRow).ToList()
dList = dList.OrderBy(Function(q) Integer.Parse(q.Cells(0).Value.ToString().Split("-").Last)).ToList()
DataGridView1.DataSource = dList.Select(function(x) x.DataBoundItem).ToList()发布于 2017-02-23 22:01:13
有 Control
通常,最简单和最通用的方法是处理DataGridView.SortCompare事件:
Private Sub DataGridView1_SortCompare(sender As Object, e As DataGridViewSortCompareEventArgs) Handles DataGridView1.SortCompare
If e.Column.Index <> 0 Then Return ' custom sort only for the first column
e.SortResult = Len(e.CellValue1).CompareTo(Len(e.CellValue2)) ' compare by string length
e.Handled = e.SortResult <> 0 ' to use the default comparison if same length
End Sub另一个选项是将IComparer传递给DataGridView.Sort方法(.Create需要.NET 4.5):
DataGridView1.Sort(Comparer(Of DataGridViewRow).Create(
Function(r1, r2)
Dim o1 = r1.Cells(0).Value, o2 = r2.Cells(0).Value
Dim i = Len(o1).CompareTo(Len(o2))
If i = 0 Then i = o1.ToString.CompareTo(o2)
Return i
End Function))https://stackoverflow.com/questions/42402415
复制相似问题