VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



DataGrid Sort

by Daniel Gaudin (1 Submission)
Category: Databases/Data Access/DAO/ADO
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (15 Votes)

Sorts the records in a datagrid form when clicking on the column header. Toggles between ascending/descending sort order.

Assumes
I assume that the connection is established and that the recordset has been opened. Besides that I would really appreciate your feedback. Let me know if you have other ideas on how to accomplish this. I'd specially like to know if there is a "nicer" alternative to the nested if-statement.
Side Effects
Not that I could see ;)
API Declarations
Dim WithEvents adoPrimaryRS As Recordset

Rate DataGrid Sort

Private Sub grdDataGrid_HeadClick(ByVal ColIndex As Integer)
 Dim strColName As String
 Static bSortAsc As Boolean
 Static strPrevCol As String
 
 strColName = grdDataGrid.Columns(ColIndex).DataField
 
' Did the user click again on the same column ? If so, check
' the previous state, in order to toggle between sorting ascending
' or descending. If this is the first time the user clicks on a column
' or if he/she clicks on another column, then sort ascending.
 If strColName = strPrevCol Then
  If bSortAsc Then
   adoPrimaryRS.Sort = strColName & " DESC"
   bSortAsc = False
  Else
   adoPrimaryRS.Sort = strColName
   bSortAsc = True
  End If
 Else
  adoPrimaryRS.Sort = strColName
  bSortAsc = True
 End If
   
 strPrevCol = strColName
End Sub

Download this snippet    Add to My Saved Code

DataGrid Sort Comments

No comments have been posted about DataGrid Sort. Why not be the first to post a comment about DataGrid Sort.

Post your comment

Subject:
Message:
0/1000 characters