Board index » delphi » A Couple of Stupid Questions...

A Couple of Stupid Questions...

These are probably really trivial but I can't find the answers anywhere.
1. If you attach a DBGrid to a TQuery, how do you know how many records
have been retrieved?
2. Within a DBGrid, how can you tell which is the currently selected
record?
Any help will be gratefully received!

 

Re:A Couple of Stupid Questions...


Quote
Alison McInnes wrote:

> These are probably really trivial but I can't find the answers anywhere.
> 1. If you attach a DBGrid to a TQuery, how do you know how many records
> have been retrieved?
> 2. Within a DBGrid, how can you tell which is the currently selected
> record?
> Any help will be gratefully received!

For the first point I wouldn't like to say for sure, but I guess the Query
RecordCount property?  This will be sensitive to the particular engine you are
talking to (some may not support it properly or at all).

The second question is easier though, but confused me for a while (and was one
of the reasons I dumped the BDE and TDB controls).  It is not the Grid which has
a row selected so much as the Query - when the user selects a row or cell in the
Grid, the fields defined by your dataset contain the data for that row.

If anyone wants to correct me on these points, please do so - like I say, I
don't use the BDE or TDB controls (shall I say that one more time <g>) but offer
these suggestions with the hope of at least pointing in the right direction.

Hope this helps.
--
Jolyon Smith                         |  The views represented above are my own
Application Development Consultant   |  and do not necessarily represent the
PLATINUM Solutions (UK)              |  views of my employer.

Re:A Couple of Stupid Questions...


Quote
Alison McInnes wrote:

> These are probably really trivial but I can't find the answers anywhere.
> 1. If you attach a DBGrid to a TQuery, how do you know how many records
> have been retrieved?
> 2. Within a DBGrid, how can you tell which is the currently selected
> record?
> Any help will be gratefully received!

You must look in the query component not in the grid.
Number of records = YourQuery.RecordCount
and
Currently selected record is automaticali updated while using a
grid and is identified by its Key in the query's dataset

Hope this help

Atle

Re:A Couple of Stupid Questions...


Quote
Alison McInnes wrote:

> These are probably really trivial but I can't find the answers anywhere.
> 1. If you attach a DBGrid to a TQuery, how do you know how many records
> have been retrieved?

Depends. For SQL tables, you have to execute the separate Count(*)
query, for local tables the Query.Recordcount property should work fine.

Quote
> 2. Within a DBGrid, how can you tell which is the currently selected
> record?
> Any help will be gratefully received!

Again, depends.

1. record relative to top of grid: derive a new component from TDBGrid
and declare "Row" property in public part - it will give you the needed
info

2. record relative to start of table:
- SQL tables: no way
- Paradox tables: there's a BDE function that returns this info, see
Delphi FAQ
- DBase table: same as for Paradox tables, if no index is active

Hope this helps!

--
-------------------------------------
Robert Cerny - application designer & developer
Neosys Ltd. Ljubljana
Email: robert.ce...@neosys.xrs.si

Re:A Couple of Stupid Questions...


Quote
Alison McInnes wrote:

> 1. If you attach a DBGrid to a TQuery, how do you know how many records
> have been retrieved?
> 2. Within a DBGrid, how can you tell which is the currently selected
> record?

1. Check Query1.RecordCount. For example, you could fill an
edit field with the total recs with a 'Edit1.Text := Query1.RecordCount;'
command.
2. The currently selected record will be indicated by a pointer if you've
left the default DBGrid1.Option with dgIndicator = True.

Re:A Couple of Stupid Questions...


In <31BDDB94.5...@SRV1.JIG.ED.AC.UK> Alison McInnes

Quote
<ALI...@SRV1.JIG.ED.AC.UK> writes:

>These are probably really trivial but I can't find the answers
anywhere.
>1. If you attach a DBGrid to a TQuery, how do you know how many
records
>have been retrieved?

Use Query1.RecordCount which returns a count of type integer

Quote
>2. Within a DBGrid, how can you tell which is the currently selected
>record?
>Any help will be gratefully received!

Look at the arrow on the left side.  That is the current record.

Re:A Couple of Stupid Questions...


Quote
In article <4qd3i0$...@dfw-ixnews4.ix.netcom.com> arr...@ix.netcom.com(Arrow) writes:
>From: arr...@ix.netcom.com(Arrow)
>Subject: Re: A Couple of Stupid Questions...
>Date: 21 Jun 1996 03:05:36 GMT
>In <31BDDB94.5...@SRV1.JIG.ED.AC.UK> Alison McInnes
><ALI...@SRV1.JIG.ED.AC.UK> writes:

>>These are probably really trivial but I can't find the answers
>anywhere.
>>1. If you attach a DBGrid to a TQuery, how do you know how many
>records
>>have been retrieved?
>Use Query1.RecordCount which returns a count of type integer
>>2. Within a DBGrid, how can you tell which is the currently selected
>>record?
>>Any help will be gratefully received!
>Look at the arrow on the left side.  That is the current record.

or refer to the tfield object that is visible in the grid. It will contain the
val;ue of the current col & row of the grid.

HTH

Boris Ingram, Cyborg Software
boris...@iafrica.com
100076.3...@compuserve.com
http://www.pcb.co.za/users/borising/cyborg.htm

Re:A Couple of Stupid Questions...


Quote

For the second, I think in the the TDBGrid there are two properties called
SelectedField and SelectedIndex which you can use to determine which field you
have selected and reterived the value.

First you use SelectedField to make sure which FieldName is selected, ie

        if dbgrid.selectedfield.fieldname = 'xxxx' then
        begin
                code here
        end

then if you want to get the value of selected field you can use the following
code,

                dbgrid.fields [selectedindex]

combine with one of the as... functions

Mike Liang
m...@tknet.tku.edu.tw

Other Threads