En Visual Basic 6, podemos ordenar rápidamente un Datagrid haciendo click en la cabecera con solo incluir esta función:
Public Sub OrdenarDataGrid( ByVal ColIndex As Integer, RS As ADODB.Recordset, Datagrid As Datagrid) Dim strColName As String Static bSortAsc As Boolean Static strPrevCol As String strColName = "[" & Datagrid.Columns(ColIndex).DataField & "]" If strColName = strPrevCol Then If bSortAsc Then RS.Sort = strColName & " DESC" bSortAsc = False Else RS.Sort = strColName bSortAsc = True End If Else RS.Sort = strColName bSortAsc = True End If strPrevCol = strColName Exit Sub
Posteriormente desde el método HeadClick del DataGrid, podemos llamar a esta función de esta forma:
Private Sub DataGrid_HeadClick(ByVal ColIndex As Integer) ' Le envia el ColIndex (columna donde hacemos click), ' el recordset enlazado al Datagrid y el mismo Datagrid Call OrdenarDataGrid(ColIndex, RSenlazado, DataGrid) End Sub
Esta función puede ser llamada desde cualquier DataGrid de nuestro proyecto con sólo añadirla a un módulo. Eso sí, hay que tener en cuenta que no funciona para ordenar campos Memo.