Solved

Current record moves off screen in TDBGrid

Posted on 2007-04-06
2
194 Views
Last Modified: 2011-10-03
Hi all,

I have a simple data capture app (Delphi 5), which uses only master/ detail TADOTables and TDBNavigator components to manipulate data in a SQL Server (2000) database. The app uses TDBGrids connected to TADOQueries on each data capture page, to give the data capturer some context w.r.t. the data being entered. The queries typically display information taken from some of the tables which are being edited on any one data capture page in the app - it so happens that on some pages the entries in the query have a one to one relationship with the data in the Master dataset of that page.

I would like to have the record in the query corresponding to the current record in the master table highlighted. I have written the following code to do this:

--snip--

private
   CurrentPos: Integer;

--snip--

{This is the datasource connected to the Master dataset}
procedure TForm3.DataSource1DataChange(Sender: TObject; Field: TField);
begin
   CurrentPos := DataSource1.DataSet.RecNo;
end;

--snip--

procedure TForm3.DBGrid4DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if DBGrid4.DataSource.DataSet.RecNo = CurrentPos then
  begin
    DBGrid4.Canvas.Brush.Color := $006495ED;
    DBGrid4.DefaultDrawColumnCell(Rect,DataCol,Column,State);
  end;
end;

--snip--

My problem is that there are more records than available space on the grid, which causes the highlighted records to move off the page. The grid however, does not automatically scroll forward, causing the context I want to provide to the data capturer to be lost.

Is there any way of getting the grid to scroll forward when the current record goes off the page?

Lourens
0
Comment
Question by:lourens_walters
2 Comments
 

Author Comment

by:lourens_walters
ID: 18865198
Hi,

I have managed to solve the problem myself. Seeing as the record numbers in the master table correspond to those in the query, I used the following code to set record numbers in the DBGrid to correspond to those in the query:

procedure TForm3.DataSource1DataChange(Sender: TObject; Field: TField);
begin
   if (ADOQuery5.Active) then
      DBGrid4.DataSource.DataSet.RecNo := DataSource1.DataSet.RecNo;
end;

DataSource1 here is the datasource attached to the master table. When data changes I set the Record number for the DBGrid datasource i.e. the query, to correspond to the Master table. This ensures that the correct record in the DBGrid is highlighted.

Lourens
0
 

Accepted Solution

by:
EE_AutoDeleter earned 0 total points
ID: 18998859
lourens_walters,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 89
How to open a new windows chrome resized and disabled the toolbar? 3 113
LAN or WAN ? 11 90
Delphi inherited method 6 68
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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