DBGrid Sorting by Clicking on the Header Row 
Author Message
 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



Tue, 29 Aug 2000 03:00:00 GMT  
 DBGrid Sorting by Clicking on the Header Row

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



Wed, 30 Aug 2000 03:00:00 GMT  
 DBGrid Sorting by Clicking on the Header Row

Or maybe
.......
MyDataCtl.RecordSource = "Select  ...  order by " &
MyDataCt1.Columns(ColIndex).DataField
.......
Peter
--



Thu, 31 Aug 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help: DBGrid - how to make DBGrid.row = current row when I click the a cell

2. Help: DBGrid - how to make DBGrid.row = current row when I click the a cell

3. Sorting the content of list view on clicking column headers

4. sort a listview by column header click

5. ? 3014 Can't open any more tables - Click header to sort by column fails

6. Sorted a listview on header click

7. List View click on header to sort date column

8. List View - Sort by Clicking Header

9. Headers selection (FlexGrid), collection sort, DBGrid vs FlexGrid

10. Header click not causing event on DBgrid.ocx?

11. Right clicking on DBGrid Header?

12. Dbgrid clicking on a row and filling teksboxes ...;

 

 
Powered by phpBB® Forum Software