To do what you want, use the grid's Mouse Events and the ColContaining/RowContaining methods:
Option Explicit
Private Enum eGrdMouseButton
gmbNone = 0
gmbRight
gmbLeft
gmbMiddle
End Enum
Private GridMouseButtonUsed As eGrdMouseButton
Private Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim iButtonRight As Integer
Dim iButtonLeft As Integer
Dim iCol As Integer
Dim iRow As Integer
iButtonRight = (Button And vbRightButton) > 0
iButtonLeft = (Button And vbLeftButton) > 0
iCol = DataGrid1.ColContaining(X)
iRow = DataGrid1.RowContaining(Y)
If iRow = -1 And iCol >= 0 Then
If iButtonRight Then
DataGrid1.SelStartCol = iCol
GridMouseButtonUsed = gmbRight
ElseIf iButtonLeft Then
GridMouseButtonUsed = gmbLeft
End If
End If
End Sub
Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If GridMouseButtonUsed Then DataGrid1_HeadClick DataGrid1.ColContaining(X)
End Sub
Private Sub DataGrid1_HeadClick(ByVal ColIndex As Integer)
If GridMouseButtonUsed = gmbLeft Then
Debug.Print "Sort ASC"
DataGrid1.SelStartCol = -1
ElseIf GridMouseButtonUsed = gmbRight Then
Debug.Print "Sort DESC"
DataGrid1.SelStartCol = -1
Else
Debug.Print "Nothing done"
End If
GridMouseButtonUsed = gmbNone
End Sub
Private Sub DataGrid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
GridMouseButtonUsed = gmbNone
End Sub
There is some extra code to help take care of situations where a user may be just marking columns
(marking a single column may still not work as expected), and additional code to make the left and
right clicks act the same (otherwise, the left click marks the column blue and the right click doesn't)
[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!