Solved

Dbctrlgrid record positioning

Posted on 2003-11-11
18
1,286 Views
Last Modified: 2010-04-03
I have a dbCtrlGrid linked to an ado datasource. After the user updates a record I re- query the datasource and the record updated is not returned by the query. I am trying to position the next record in the dataset on the panel that was active previously. The code below does not give the correct results
Any help would be appreciated.


      adoDiary.next;  // move to next record
      recordno := adoDiary.fieldbyname('ref').asinteger; // save record number
      activePanel := dbCtrlgrid1.PanelIndex;    // save index of current active panel
      doquery;                                               // re query data
      adoDiary.Locate('ref', recordno, []);        // locate record
      dbCtrlgrid1.PanelIndex := activePanel;    //



Regards

Alan
0
Comment
Question by:alanjbrown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 4
18 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9721422
usual you don't need to query again

just use the requery-method of the dataset

i don't know what your doQuery does,
but if you do so the query must be closed and
reopened for fetching new

meikl ;-)
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9721451
I don't seem to have a requery method. I am using Delphi4 with Adonis Ado components linked to an MS Sql 7 database.

The query shows records where a certain field is Null after data has been entered in that field it should not be displayed on the grid.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9721470
oh, no standard ado,
well, then there may be no requery-method

??
>The query shows records where a certain field is Null
>after data has been entered in that field it should not be displayed on the grid.

i don't understand, why do u use locate then?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:alanjbrown
ID: 9721491
I move to the next record before re querying the data, I then store the reference number of the next record after the one updated. I then requery the data and return all the records that meet the criterior. Locate then moves to the actual record that I think is the one that the user will be interested in, but all the records meeting the criterior are displayed. I then try to position this record in the same position on the screen that was occupied before the requery.

0
 
LVL 17

Expert Comment

by:geobul
ID: 9721789
Hi,

Does your ado query have MoveBy method? if yes then:

      ...
      recordno := adoDiary.fieldbyname('ref').asinteger; // save record number
      activePanel := dbCtrlgrid1.PanelIndex;    // save index of current active panel
      doquery;
      adoDiary.MoveBy(recordno);
      dbCtrlgrid1.PanelIndex := activePanel;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 9721800
Oops,
recordno := adoDiary.fieldbyname('ref').asinteger; // save record number

should be

recordno := adoDiary.RecNo;

Regards, Geo
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9722037
Geo,

yes I do have a Moveby method.

I used adoDiary.fieldbyname('ref').asinteger because it uniquely identifies a record whereas using adoDiary.RecNo may not return the same record the next time the query is run as other users may have added, deleted or amended records in the underlying table.

Alan
0
 
LVL 17

Expert Comment

by:geobul
ID: 9722160
Yes, but using the same logic your adoDiary.fieldbyname('ref').asinteger record could have been deleted meanwhile. Perhaps combination could be a better solution. Remember the ref and the recno in different variables. Then first search for 'ref' and if locate returns false use RecNo.

What type is your 'ref' field in the database table? You said that Locate method doesn't work. Well, make it yourself:

function MyLocate(DataSet: TDataSet; FieldName: string; AValue: Variant): boolean;
begin
  result := false;
  with DataSet do begin
    First;
    while not eof do begin
      if FieldByName(FieldName).Value = AValue then begin
        result := true;
        break;
      end;
      Next;
    end;
  end;
end;

Regards, Geo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9722203
sorry, was absent,

following i thought
- you need to remember additional the record which is displayed in the first panel
- after your requery use moveby or locate for this record
-> so you have the record again which was displayed on top on top again
- step forward from this (next method) until you come to the record which was the next of the updated record

well, just a thought

meikl ;-)
0
 
LVL 17

Expert Comment

by:geobul
ID: 9722229
1. You may add to the function above:

with DataSet do begin
  SetTempState(dsFilter);
  try
    ...
  finally
    RestoreState(dsBrowse);
  end;
end;

2. If you decide to make both checks then before adoDiary.MoveBy add adoDiary.First; because when MyLocate returns false then the dataset cursor is eof.

Regards, Geo
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9722239
Hi Kretzschmar

I think your suggestion sounds as if it should give the correct result. Do you have a way of returning the 'ref' of the top record of the display?
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 500 total points
ID: 9722584
well, not tested, but
setting the panelindex to 0,
the dataset should move to this there displayed record,
where u can then get some unique info for a later locate

simplified code flow:

var
  curIndex, topref, TargetRef : Integer;
begin

  do_yourUpdate;

  curIndex := dbctrlGrid1.panelIndex;
  dbctrlGrid1.panelIndex := 0;
  topref := dataset.fieldbyname('Ref').asinteger;
  dbctrlGrid1.panelIndex := curIndex;
  dataset.next;
  targetref := dataset.fieldbyname('Ref').asinteger;

  do_requery; //do requery

  dataset.locate('Ref',TopRef,[]);
  //well now we have to ways

  //simplest
  dbctrlGrid1.panelIndex := curIndex;

  //or
  while (not(dataset.eof)) and
          (dataset.fieldbyname('Ref').asinteger <> TargetRef) do
     dataset.next;
...

just from head, typos possible

meikl ;-)
 
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9723048
Kretzschmar

That looking good!
 UnfortunatlyI won't be able to test it until tommorrow.
One final point, how can I switch off the updating of the display during the process?

Alan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9723134
>UnfortunatlyI won't be able to test it until tommorrow.
dito, i can't test my suggestion myself until tomorrow :-))

we will see, if it works

meikl ;-)
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9730837
Kretzschmar

It seems to work. Thank you very much.

I will award the points very soon. Is it possible to switch off the updating of the dbctrlGrid during the proccess.


Alan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9730894
>It seems to work.

glad to hear this,
sorry, i didn't got the time to test myself

>Is it possible to switch off the updating of the
>dbctrlGrid during the proccess

try to use

dataset.disableControls;
try
  ...
finally
  dataset.enableControls;
end;

but i don't know, if then,
dbctrlGrid-methods are reflected to the dataset
-> do a try

meikl ;-)
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9731006
Kretzschmar

Unfortunatly disableControls stops the system working. However I am very happy with the result even without this as the effect of the movement of the ctrlgrid is hardly noticable on a fast system. I have awarded you the points
Thank you for your help, it was much appreciated.


Alan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9731077
well, thanks for the points, glad to helped you :-))

sorry, that it will not work with disable-controls,
u could also try

LockWindowsUpdate(dbctrlGrid1.handle);
try
  ... do your stuff here
finally
  LockWindowsUpdate(0);
end;

but this may have the side-effect,
that the desktop is refreshing its icons after
  LockWindowsUpdate(0);

meikl ;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question