
DBGrid Sorting by Clicking on the Header Row
Joe,
There is an event in DBGrid called <object>_HeadClick. It is triggered when someone clicks on the header of a column. It's argument is the column index. I've used it successfully with a bound grid as follows:
In this example, the grid is bound to a datacontrol called MyDataCtl. The data control is using an SQL query (dynaset) as it's recordsource. When a column is clicked, I simply requery the table using a different ORDER BY clause. So far it has worked well for my application.
Also note below -- I have used the SelChange event to cancel any column selection. I noticed that clicking on a column header caused the entire column to become "selected" (inverted color), which I didn't want. The Cancel of the SelChange event stopped that. Actually, sometimes the column flashes for a second (inverted), but then it ends up looking normal.
----------------------------------------------------------------------------------
Private Sub MyGrid_HeadClick(ByVal ColIndex As Integer)
Dim OrderByField(3) as String
'This could take a second or two, so show the user
'that we're working...
Screen.MousePointer = vbHourglass
'Set up the order by fields to be used to sort by
'Essentially, these are the columns of the table that
'allow sorting...
OrderByField(0) = "Date"
OrderByField(1) = "LastName"
OrderByField(2) = "Salary"
'Reset the data control's recordsource and refresh the grid
'with the new query.
MyDataCtl.RecordSource = "Select ... order by " & OrderByField(ColIndex)
MyGrid.Refresh
Screen.MousePointer = vbDefault
End Sub
Private Sub MyGrid_SelChange(Cancel As Integer)
Cancel = True
End Sub
----------------------------------------------------------------------------------
Hope this helps --
Vinnie
:I'm trying to get the DBGrid (bound) control to allow a user to sort on a
:column. I'd like it to work like the Window's Explorer does, that is if you
:click on the column header it will sort all the records on that column. The
:best I've been able to delive now is to sort based on a double click somewhere
:in the column. I'd like the user to be able to click on the column header.
:I've tried to check if the click is on .row = -1 but it crashes there?
:
:Anyone ever done that? Much thanks in advance
:Regards,
:Joe Nizolak
:
:"The scientific name for an animal that doesn't either run from or fight
:its enemies is lunch." -- Michael Friedman