How to hook after a query is executed?

969 views Asked by At

I am using AnyDac and TMS TAdvDbGrid (rather than plain TDbGrid), in case that makes any difference, but I suspect that there is a general solution.

I have a data aware grid, which should display a maximum of say 12 lines because of screen size. If the associated query returns less than 12 rows then I would like to change the DB Grid's height property accordingly (leaving some empty space below the newly poisiotned bottom og the grid, rather than within it).

I can post the code for that if requested, but my question is what event handler should I do it in?

I had thought to use the Query's AfterOpen() handler, but that is never trigeered.

What else is there? I could use the DB Grid's DrawCell() method, but that would be called frequently.


[Update] I added some tracing. I clciked on the master grid exactly once (its OnClick() gets called twice, but I tried this a few times and it is consistent).

Here's the trace:

2   00:00:25.225 [Detail grid scolled]  11:02:12 AM
2   00:00:25.228 [Detail grid scolled]  11:02:12 AM
2   00:00:25.229 [Detail grid scolled]  11:02:12 AM
2   00:00:25.231 [Detail grid scolled]  11:02:12 AM
2   00:00:25.233 [Detail grid scolled]  11:02:12 AM
2   00:00:25.234 [Clicked master grid]  11:02:12 AM
2   00:00:25.234 [Master grid scolled]  11:02:12 AM
2   00:00:25.325 [Clicked master grid]  11:02:12 AM

I notice a few things:

  • The detail grid is reported as being scrolled before the master grid is repoted as being clicked. I gues that's just the Windows message queue, but it does seem strange.

  • The master grid is reported as being clicked twice

  • Those 2 clicks seem to cause 2 sets of scrolling. The first reports scrolling 5 times; by coincidence (?) there are 5 columns on the detail grid. If there were more, or if processing on scroll was heavy, that ight be an overhead (and I can't understand enough to set & clear flags). Ideally I would like to hook that 2nd "group" which has only a single click, but I don't knwo how.

  • I can confirm AfterScoll as agood event to hook. It is only called when I clcik on a master grid row which is not the current row (i.s, when the content of the detail grid chages).

Any comments?


[Update++]

Since i am using an AnyDac TADQuery, I found that I can assign OrdersADQuery.Command.AfterOpen := OrdersADQueryAfterOpen; and that function will be called exactly once when I click a non-current row of the master grid.

That works for me, but fell free to comment on my previous upate if you think that it will help others

1

There are 1 answers

6
MartynA On BEST ANSWER

Although there are several other possibilities, in my experience the AfterScroll event of the dataset populating the grid is the most reliable good place to catch changes to the number of rows, etc, and movement of the dataset cursor. You didn't say whether you wanted to cover deletions and inserts, btw.