Solved

Dbctrlgrid record positioning

Posted on 2003-11-11
18
1,179 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
  • 7
  • 7
  • 4
18 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:alanjbrown
Comment Utility
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
Comment Utility
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
Comment Utility
Oops,
recordno := adoDiary.fieldbyname('ref').asinteger; // save record number

should be

recordno := adoDiary.RecNo;

Regards, Geo
0
 
LVL 1

Author Comment

by:alanjbrown
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 17

Expert Comment

by:geobul
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now