Recent

Author Topic: [SOLVED] Selecting/give focus of the current record in a TDBGrid  (Read 3263 times)

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[SOLVED] Selecting/give focus of the current record in a TDBGrid
« on: December 14, 2015, 03:21:08 pm »
I am using the following code to determine the ID of a game closest to the current date.
Code: Pascal  [Select][+][-]
  1.  
  2.   cSQL := 'SELECT Wed_ID, Wed_Datum, Wed_Tijd ' +
  3.           'FROM tbl_Wedstrijden ' +
  4.           'WHERE Date(Wed_Datum) > Date() ' +
  5.           'ORDER BY Wed_Datum, Wed_Tijd';
  6.  
I am also using a DBGrid_Wedstrijden: TDBGrid; which contains the actual games and so on
How can i select by code the correct game?
« Last Edit: December 14, 2015, 09:22:56 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Selecting/give focus of the current record in a TDBGrid
« Reply #1 on: December 14, 2015, 06:34:49 pm »
I'll try to understand what you mean. DBGrid_Wedstrijden has the output of your query. Is it not the same data, you can use Tdataset.locate() to find your record.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

balazsszekely

  • Guest
Re: Selecting/give focus of the current record in a TDBGrid
« Reply #2 on: December 14, 2015, 06:49:16 pm »
I assume you want to locate a specific record in the grid. Since all grids has a datasource and all datasources has a dataset,  use the locate function of the dataset like this:
Code: Pascal  [Select][+][-]
  1.   SQLQuery.Locate('Wed_ID', IDToFind, []);
For more complex search you can loop through the dataset(first disable the update of the visual controls):
Code: Pascal  [Select][+][-]
  1. function TForm1.LocateInGrid(const ID: Integer): Boolean;
  2. var
  3.   SQLQuery: TSQLQuery;
  4.   Orig_ID: Integer;
  5. begin
  6.   Result := False;
  7.   SQLQuery := (DBGrid_Wedstrijden.DataSource.DataSet as TSQLQuery);
  8.   Orig_ID := SQLQuery.FieldByName('Wed_ID').AsInteger;  
  9.   SQLQuery.DisableControls;
  10.   try
  11.     SQLQuery.First;
  12.     while not SQLQuery.EOF do
  13.     begin
  14.       if SQLQuery.FieldByName('Wed_ID').AsInteger = ID then //if you need add more conditions here
  15.       begin
  16.         Result := True;
  17.         Break;
  18.       end;
  19.       SQLQuery.Next;
  20.     end;
  21.     if not Result then
  22.       SQLQuery.Locate('Wed_ID', Orig_ID, []);
  23.   finally
  24.     SQLQuery.EnableControls;
  25.   end;
  26. end;              
  27.  
« Last Edit: December 14, 2015, 06:52:51 pm by GetMem »

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Selecting/give focus of the current record in a TDBGrid
« Reply #3 on: December 14, 2015, 09:22:41 pm »
I was on the right track but i was looking at the wrong query.
i needed to look at TQ_Wedstrijden instead of TQ_Rapport :(


This is my final version of what i need:
Code: Pascal  [Select][+][-]
  1.   TQ_Wedstrijden.Active := True;
  2.  
  3.  
  4.   TQ_Wedstrijden.Last;
  5.   TQ_Wedstrijden.First;
  6.  
  7.  
  8.   GroupBox_Wedstrijden.Caption :=
  9.     Format('Games (%d)', [TQ_Wedstrijden.RecordCount]);
  10.  
  11.  
  12.   // Bepaal de wedstrijd die het dichtstbij de huidige datum ligt.
  13.   cSQL := 'SELECT Wed_ID, Wed_Datum, Wed_Tijd ' +
  14.           'FROM tbl_Wedstrijden ' +
  15.           'WHERE Date(Wed_Datum) > Date() ' +
  16.           'ORDER BY Wed_Datum, Wed_Tijd';
  17.  
  18.  
  19.   with Form_Information do begin
  20.     TQ_Rapport.Active := False;
  21.     TQ_Rapport.DataBase := Connect_RefereeDB;
  22.     TQ_Rapport.SQL.Text := cSQL;
  23.     TQ_Rapport.Active := True;
  24.     TQ_Rapport.Last;
  25.     TQ_Rapport.First;
  26.     if TQ_Rapport.RecordCount > 0 then begin
  27.       wi := TQ_Rapport.FieldByName('Wed_ID').AsInteger;
  28.       showmessage ('Wed_ID = ' + IntToStr(wi));
  29.       TQ_Wedstrijden.Locate('Wed_ID', TQ_Rapport.FieldByName('Wed_ID').AsInteger, []);
  30.     end;  // if
  31.   end;  // with
  32.  
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: [SOLVED] Selecting/give focus of the current record in a TDBGrid
« Reply #4 on: December 15, 2015, 09:16:00 am »
Why creating a local variabele if you don't need it?
Code: [Select]
showmessage ('Wed_ID = ' + TQ_Rapport.FieldByName('Wed_ID').AsString);
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: [SOLVED] Selecting/give focus of the current record in a TDBGrid
« Reply #5 on: December 15, 2015, 10:15:54 pm »
it was to check the value while in runtime.
But yes it's obsolete
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

 

TinyPortal © 2005-2018