5

I have two DB grids on a form and one has a vertical scrollbar and the other doesn't, even when the DataSource query returns more results than the Grid's visible rows.

Why is the vertical scrollbar on the TDBGrid not displayed?


[Update] Delphi XE2 starter, using AnyDac.

When I first start the program, the DB grid does have a vertical scrollbar, but later it vanishes and I have narrowed it down, but still cannot understand how to correct teh problem.

I have two MySql tables and two DBgrids. One is an overview of all test runs and the other contains details of measurements taken during a test run. When the user clicks on a row of the "summary" grid, I update a parameter of the query of the "details" grid. This works and does not remove the scroll bar.

The problem comes when I start a new tets run and insert a new row into the "summary" table - the scroll bar of the "details" table vanishes!

mysql> describe test_runs;
+------------------+-------------+------+-----+-------------------+----------------+
| Field            | Type        | Null | Key | Default           | Extra          |
+------------------+-------------+------+-----+-------------------+----------------+
| run_id           | int(11)     | NO   | PRI | NULL              | auto_increment |
| start_time_stamp | timestamp   | NO   |     | CURRENT_TIMESTAMP |                |
| end_time_stamp   | timestamp   | YES  |     | NULL              |                |
| description      | varchar(64) | YES  |     | NULL              |                |
+------------------+-------------+------+-----+-------------------+----------------+
4 rows in set (0.02 sec)

mysql> describe measurements;
+------------------------+-----------+------+-----+-------------------+-------+
| Field                  | Type      | Null | Key | Default           | Extra |
+------------------------+-----------+------+-----+-------------------+-------+
| run_id                 | int(11)   | NO   | MUL | NULL              |       |
| measurement_time_stamp | timestamp | NO   |     | CURRENT_TIMESTAMP |       |
| ph                     | float     | NO   |     | NULL              |       |
| conductivity           | float     | NO   |     | NULL              |       |
| cod                    | float     | NO   |     | NULL              |       |
+------------------------+-----------+------+-----+-------------------+-------+
5 rows in set (0.04 sec)

mysql>

The AnyDac queries are, repectively,

SELECT 
   run_id,
   start_time_stamp,
   end_time_stamp,
   CONCAT(CONCAT(CONCAT(CONCAT(LPAD(EXTRACT(HOUR FROM timediff(end_time_stamp,start_time_stamp)), 2, '0'), ":"),LPAD(EXTRACT(MINUTE FROM timediff(end_time_stamp,start_time_stamp)), 2, '0'), ":"), LPAD(EXTRACT(SECOND FROM timediff(end_time_stamp,start_time_stamp)), 2, '0'))) AS duration,
   description    
FROM 
   test_runs 
ORDER BY 
   start_time_stamp 
DESC

and

SELECT 
   run_id,
   measurement_time_stamp, 
   ROUND(ph, :float_precision) as ph, 
   ROUND(conductivity, :float_precision) as conductivity, 
   ROUND(cod, :float_precision) as cod    
FROM 
   measurements    
WHERE 
   run_id=:run_id
ORDER BY 
   measurement_time_stamp 
DESC

As you can see, there is referce in the measurements table to the test_runs table

When I execute the following SQL, the scroll bar of the measurements table vanishes:

INSERT INTO 
   test_runs (start_time_stamp, description) 
VALUES 
   (CURRENT_TIMESTAMP, "<Currently running test>"); 

SELECT LAST_INSERT_ID() AS run_id

Can anyne tell me what I am doing wrong? Thanks a 0x000F4240


[Update] Once again, the problem is that when I add a row to the test_runs table, the vertical scroll bar of the DB grid which reflects the query of the measurements table vanishes.

So, somehow, one DB grid/dataset/table is affecting the otherDB grid.

The data in the actual MySql database are fine. The problem occurs no matter whether I use a command to INSERT INTO and two decicated queries (with SQL fixed at design time and never changed) or if I use @kobik suggestion to dataset.append.

The database contents are fine, the DB contents are correct, when I click on the summary test_runs DB grid the contents of the details measurements DB grid are correctly updated.

The only problem is that inserting a row into the table of the test_runs query causes the scroll bar to vanish from the DB grid of the measurements DB grid.

When I comment that out, I can click the test_runs DB grid and swap between test runs without the scroll bar vanshing, but can never get back to the current test, because there is no entry for it in the DB grid.

When I first start a new test dataset.RecordCount` is, of course, zero - which might cause the scrollbar to vanish?

However, as measurements are made (every one second, timer based), I have checked and the value of dataset.RecordCount` increments, so here ought to be a scrollbar after the second measurement? Or do I have to force it to appear once the control has decided that it was not needed? (Invalidating the DB grid did not cause the missing scrollbar to appear)


[Update] As suggested by @kobik I have made run_id a primary, auto increment key on the test_runs table and an index on the measurements table. I have updated the table details given above. I have also set the mastersource of the query which populates the test drun DB grid to the datasource of the test runs "summary" DB grid's query.

I delete the databse (my code auotmatically recreates it on first run). At first, neither DB grid had a vertical scroll bar, becaus they were both empty. When I added a first test the "summary" DB grid had no scroll bar, because it had only 1 entry, the "details" DB grid had no scrollbar at 0 and entries and the scroll bar appeared with the 2nd set of measurements.

I added a second test run and the "sumamry" DB grid had a scroll bar as it now had two entries, but as soon as I added the second entry to the summary, the scroll bar vanished from the "details" DB grid (which it did not do when there was a single test run), no matter how many entries it had (I breakpoint as each set of measuements is added and check measurementsQuery.RecordCount and see it go 0, 1, 2 ...)

It is worth pointing out that this only happens when I add a new test run (other than the first)). If I launch the program, both DB grids have scroll bars as expectd (> 1 entries). And I can click or mousewheel through the summary to disaply the details of the coresponding test run. The scroll bar never vanishes - until I add a new test run, which inserts a row ito the summary DB grid.

[Update]
Please see https://stackoverflow.com/questions/15399769/why-is-the-vertical-scrollbar-on-a-tdbgrid-not-displayed-redux


Community
  • 1
  • 1
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 1
    Check the answer to this [question](http://stackoverflow.com/questions/7529524/keep-the-scrollbars-hidden-in-a-delphi-dbgrid-even-on-resize) and use `ShowScrollbar(DBGrid.Handle, SB_VERT, true)` in the overriden `UpdateScrollbar`. – Guillem Vicens Mar 05 '13 at 12:07
  • @GuillemVicens +1 What I don't understand is that one of my DB grids ides this & the other does not. I guess it's a property. I will delete the 2nd, copy the first & rename & see how that goes & get back to you. AS er your solution, I have no code to do that for the 1st grid, which does have scrollbars. Thanks for trying to help – Mawg says reinstate Monica Mar 05 '13 at 14:08
  • 4
    I can't reproduce this problem, and you've given no details which would allow us to do so. Every DBGrid I've placed on a form automatically gets vertical (and horizontal) scrollbars when the rows or columns exceed the visible space, and I can't find a way to cause that not to happen. There's not even anything in the Object Inspector or DBGrid.Options that would allow this to be configured intentionally; this means it's most likely something in your code, or you're not getting the number of rows back you think you are from your dataset. – Ken White Mar 06 '13 at 00:30
  • 3
    @Ken, even more, showing of that scrollbar [`is hardcoded`](http://stackoverflow.com/a/9924347/960757) and depends on displayed rows and it's not so easy to hide it. – TLama Mar 06 '13 at 00:41
  • 1
    I would definitely avoid using `INSERT` on a datasource linked with your DB grid. Though I'm not saying it's the reason of what you're describing, but might be. – TLama Mar 07 '13 at 11:03
  • @TLama +1 but, that totally flummoxed me!! You mean that I can't/shouldn't use a DB grid for a table to which I am adding rows? I should only look at staic/dead tables and not living ones? Doesn't that take away much of the usefulness? – Mawg says reinstate Monica Mar 07 '13 at 11:13
  • Wah! I wasn't even aware of `DataSet.Append/DataSet.Post` and will check it out. The dataset is not closed and I close/open after inserting - but what I don't understand is the visual effect - the data are added and scroll down, but no scroll bar so I can't see the older rows. – Mawg says reinstate Monica Mar 07 '13 at 11:26

2 Answers2

9

After reading your comments, and reviewing your edit question, I'm pretty sure that the TDBGrid's "details" DataSet is returning a single record after your INSERT. And that explains why there is no vertical scrollbar - TDBGrid will show a vertical scrollbar only if records > 1.

You can make a simple test by checking the DataSet.RecordCount that is linked to the grid. It should show 1.


Apparently, there is a known issue in QC: TDBGrid vertical scrollbar dissappears.
It goes all the way from D7..XE2. (Maybe even for older/newer versions).

When you have a master/detail record, and the detail is shown using a TDBGrid, then the vertical scrollbar dissappear randomly, even if there are more records than will fit! You can see the right border outline of the scrollbar though, including the thumb thingy moving. It's as if the grid has resized slightly over the scrollbar.

This might be true also for filtered DataSets.
So, You might want to implement one of the workarounds suggested in the QC.

kobik
  • 21,001
  • 4
  • 61
  • 121
  • I guess I'm just too dumb to use DB grid, because when I tried that, it changed nothing :-( Just a question - should I have to? I have two decdicated queries, one for each DB grid, who’s SQL is set at design time & never changes. When I insert into I either table I (re) use a single AnyDac TADCommand who’s text I set (toggle) at run time. I do not reuse either of the queries and there is no relationship between the data in them except that when the user clicks the summary DB grid, I set the `run_id` as parameter to the second query. – Mawg says reinstate Monica Mar 08 '13 at 05:32
  • Btw, to insert your way, I had to lose the `float_precision` parameter of my seond query (see above). Can't I just stick with inserting via command and refreshing the dedicated query as appropriate? Btw, recordcount is not 1 for either of the datasets, yet one DB grid has a scrollbar and one doesn't – Mawg says reinstate Monica Mar 08 '13 at 05:33
  • 1
    @Mawg, check record count for the Delphi `dataset`, NOT the `database` table. put a button on your form and when you click it show message `yourQry.RecordCount` (the query or dataset that is attached to your grid). – kobik Mar 08 '13 at 10:38
  • @kobik +1 Thanks, I did, please see updated question above. Basically the `Query.RecordCOunt` goes 0, 1, 2, 3 ... starting at zero when a new test run is started and incremeneting with every set of measurements. – Mawg says reinstate Monica Mar 08 '13 at 10:54
  • 1
    `"The only problem is that inserting a row into the table of the test_runs query casues the scroll bar to vanish from the DB grid of the measurements DB grid."` - this is expected. your `details` dataset contains 0 row. do you see a scroll bar when you have 2 (not 0, not 1) records in details (or when there are more rows than visible grid) – kobik Mar 08 '13 at 11:06
  • +2 I have updatd at the end of the question, for the sake of those who might be coming fresh to it. Thanks *very* much for sticking with me here and trying to help. I really do appreciate it. – Mawg says reinstate Monica Mar 09 '13 at 02:18
  • 3
    This is going out of the question scope... viewing your sources, the master/details definition is wrong. in `DetailsADQuery` you dont need to use `WHERE run_id=:run_id` - you need to set `MasterFields=run_id` and `IndexFieldNames=run_id`. and dont use `INSERT INTO`, use the `Append` method. please go over some master/detail Delphi tutorials first. once you get the idea it's really easy :) – kobik Mar 09 '13 at 14:10
  • +1 Thanks & please pardon the delay, I think that we are in very different time zones. I have don as you said (but please pardon me if I say that it feels wrong to use a query to put data IN to a database; I just want to use a command to put in and a query to take out). But, nvm, better minds than I know what they are doing. A few questions: what about an SQL `UPDATE`? How do I do that? And you will have noticed that 1 of my queries above uses SQL's `ROUND`, so now I have to move that into my code, I guess(?) and also 3 queries not shown each use SQL's `MIN`, `AVG` and `MAX`. – Mawg says reinstate Monica Mar 10 '13 at 03:33
  • p.s can I `DELETE FROM ` by Command or must that also be through query? (sorry, still reading the tutorial ;-)
    – Mawg says reinstate Monica Mar 10 '13 at 03:38
  • it looks like the bounty deadline will expire & the system will award you the points. Well, why not? You put quite a bit of effort to help me. BUT, the problem is still there. Can you please look at the question updates? Basically, adding a new test causes the measurements grid to empty (correct) and adding a new measurement every second, the vertical scrollbar is added when measurementsQury.RecordCount reaches 2. But only for the first test run. On subsequent runs, when a new test is added the measurements grid never gets a scroll bar (even though its query's measuement count reaches 2 – Mawg says reinstate Monica Mar 13 '13 at 08:43
  • And the problem is only when adding a (subesequent) test. If I have several test runs, close and reopen the program, then I can click the test runs overview (Master) grid and the measurements (Detail) grid updates, with scroll grid as it ought to be. – Mawg says reinstate Monica Mar 13 '13 at 08:45
  • @Mawg, sorry for not being able to resolve all your issues - I cant reproduce all of them. I will probably delete this answer since it has become way too localized. – kobik Mar 13 '13 at 09:42
  • Please don't. Someone has to get the points ;-) And you did put in a lot of work. I think that I will probably open a new, better phrased question after that, base don what I have learned. Also, I decded not to use Master/Detail, just parametrized queries. Tahnks for your help – Mawg says reinstate Monica Mar 13 '13 at 10:46
  • 4
    @Mawg, I don't care much about the points. this Q has become a big salad that helps no one else, and involves tons of information. You really need to put an effort and break your *problem* into smaller and accurate questions. – kobik Mar 13 '13 at 10:53
  • Thanks again for all of your help. Could you please take a look at http://stackoverflow.com/questions/15399769/why-is-the-vertical-scrollbar-on-a-tdbgrid-not-displayed-redux – Mawg says reinstate Monica Mar 14 '13 at 01:49
  • thats a 1,000,00 for coming back to the question and posting that link with the workaround – Mawg says reinstate Monica Mar 18 '13 at 02:57
0

I set the Scrollbars to ssBoth. It got fixed