Private ds As DataeSet 'DataSet/DataTable の構築部は省略 Private Sub 何某() Me.DataGridView1.DataSource = Me.ds.Tables("File") 'File テーブルがバインドされていたとする Me.DataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.Programmatic End Sub Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick If e.ColumnIndex = 0 Then Dim order = SortOrder.Ascending If Me.DataGridView1.Columns(0).HeaderCell.SortGlyphDirection = SortOrder.Ascending Then order = SortOrder.Descending End If Dim q = Me.ds.Tables("File").AsEnumerable() q = q.OrderBy(Function(r) r, New RowSorter(order, "FullPath")) 'LINQ で並び替え Me.DataGridView1.DataSource = q.CopyToDataTable() 'DataTable に復元 Me.DataGridView1.Columns(0).HeaderCell.SortGlyphDirection = order End If End Sub Private Class RowSorter Inherits Comparer(Of DataRow) Private Declare Unicode Function StrCmpLogicalW Lib "shlwapi" (x As String, y As String) As Integer Private ReadOnly order As SortOrder Private ReadOnly fieldName As String Public Sub New(order As SortOrder, fieldName As String) Me.order = order Me.fieldName = fieldName End Sub Public Overrides Function Compare(x As DataRow, y As DataRow) As Integer If order = SortOrder.Descending Then Return StrCmpLogicalW(y(fieldName)?.ToString(), x(fieldName)?.ToString()) Else Return StrCmpLogicalW(x(fieldName)?.ToString(), y(fieldName)?.ToString()) End If End Function End Class