Recent

Author Topic: [SOLVED] Any way to restrict new record being inserted by down key on DBGrid?  (Read 577 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1696
Currently a new record is inserted automatically when I press down key at the bottom of DBGrid. I'd like a new record is not inserted by pressing down key. And insert is done only by pressing (+) button of DBNavigator.

Is there any way to do this?
« Last Edit: October 16, 2025, 10:18:29 am by egsuh »

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: Any way to restrict new record being inserted by down key on DBGrid?
« Reply #1 on: October 15, 2025, 10:00:53 am »
hmmm.... untested:
Use a blocking variable (Form-Wide boolean?) which is permanently false, and only gets set to True, when clicking on "+".
Then you might be able to use the OnBeforeInsert-Event of the underlying TDataset, there checking if the Blocker is True or False
True = User clicked the "+" --> Proceed with inserting
False = User navigated in the Grid --> "abort" (Hint)

But as i said: untested

EDIT: I seem to remember there being an option for DBGrid, but for the life of me i can't remember/find it
« Last Edit: October 15, 2025, 10:10:57 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 2477
    • http://www.cdbc.dk
Re: Any way to restrict new record being inserted by down key on DBGrid?
« Reply #2 on: October 15, 2025, 10:28:15 am »
Hi
Maybe intercept the key in a GridKeyDown-Handler and the set the key to 0
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

LacaK

  • Hero Member
  • *****
  • Posts: 702
Re: Any way to restrict new record being inserted by down key on DBGrid?
« Reply #3 on: October 15, 2025, 10:52:44 am »
Can you try to set in TDBGrid.Options dgDisableInsert ?

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: Any way to restrict new record being inserted by down key on DBGrid?
« Reply #4 on: October 15, 2025, 11:13:37 am »
Hi
Maybe intercept the key in a GridKeyDown-Handler and the set the key to 0
Regards Benny
That would prevent navigating the Grid, no?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: Any way to restrict new record being inserted by down key on DBGrid?
« Reply #5 on: October 15, 2025, 11:18:14 am »
Can you try to set in TDBGrid.Options dgDisableInsert ?
Yepp, looks like that's the option i was alluding to.
Just tested it with a Project i'm currently working on, which has a DBGrid with a DBNavigator.
set the option, moving with arrow down doesn't add a new row, but clicking on "+" inserts a new row at cursor location.

Now for my own project i only have to find out, how to force the new inserted row to always appear at the bottom of the Grid, irrespective where the cursor was when user clicked on "+"

EDIT: Which alludes to "append" instead of "Insert".
Ah well, off i go hunting.....
« Last Edit: October 15, 2025, 11:59:04 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

egsuh

  • Hero Member
  • *****
  • Posts: 1696
Re: Any way to restrict new record being inserted by down key on DBGrid?
« Reply #6 on: October 16, 2025, 10:18:03 am »
Quote
Can you try to set in TDBGrid.Options dgDisableInsert ?

Yes, this does what  I want to do. Thanks a lot.
Waiting Zboni to find some way to "append" record ^^

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: Any way to restrict new record being inserted by down key on DBGrid?
« Reply #7 on: October 16, 2025, 11:55:36 am »
Quote
Can you try to set in TDBGrid.Options dgDisableInsert ?

Yes, this does what  I want to do. Thanks a lot.
Waiting Zboni to find some way to "append" record ^^
Found it: --> https://forum.lazarus.freepascal.org/index.php?topic=25438.0
Code: Pascal  [Select][+][-]
  1. procedure TFormPowerUser.dbnPowerUsersClick(Sender: TObject;
  2.   Button: TDBNavButtonType);
  3. Var m:String;
  4. begin
  5.   Case Button Of
  6.     nbInsert: Begin
  7.                 IsDirty:=True;
  8.                 (Sender as TDBNavigator).DataSource.DataSet.Append;  //THIS ONE
  9.               End;
  10.     //nbInsert,
  11.     nbEdit,
  12.     nbDelete: IsDirty:=True;
  13.     nbPost  : Begin
  14.                 SaveChanges;
  15.                 IsDirty:=False;
  16.               end;
  17.     nbCancel: Begin
  18.                 DiscardChanges;
  19.                 IsDirty:=False;
  20.               end;
  21.   end;
  22. end;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018