Recent

Author Topic: [SOLVED] DBGrid, set defaults when adding new Row  (Read 639 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
[SOLVED] DBGrid, set defaults when adding new Row
« on: September 16, 2025, 12:48:33 pm »
Hi Folks,

never worked with the DB-aware controls, but this has to be RAD

I have a DBGrid with a DBNavigator attached to it. Everything works, but.....

I have 4 columns representing Booleans in the Database.
When i add/insert a new row via DBNavigator, i get an "empty" Row in the Grid, with the 4 CheckBoxes having this "half state" checkmark.
I've been looking for an Event (or whatever) of DBNavigator or DBGrid like "OnNewRow" or whatever to set some Defaults for those 4 Columns.

Any ideas?

EDIT: Just looked at and tested OnNewRecord and AfterInsert of the DataSource.Dataset
No cigar. Those fire before the Row is added

I need something after the Row in the Grid is added
« Last Edit: September 16, 2025, 01:25:30 pm 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: 2464
    • http://www.cdbc.dk
Re: DBGrid, set defaults when adding new Row
« Reply #1 on: September 16, 2025, 01:01:20 pm »
Hi
I'm as unfamiliar with these controls as you are, but does the grid fire an 'OnChange' event?
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

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: DBGrid, set defaults when adding new Row
« Reply #2 on: September 16, 2025, 01:04:36 pm »
Hi
I'm as unfamiliar with these controls as you are, but does the grid fire an 'OnChange' event?
Regards Benny
Haven't found one, though i suspect most events are in the underlying Datasource/Dataset, since the Grid is (more or less) just a representation of the Datasource.Dataset
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

paweld

  • Hero Member
  • *****
  • Posts: 1494
Re: DBGrid, set defaults when adding new Row
« Reply #3 on: September 16, 2025, 01:11:28 pm »
Add a record with DBNavigator causes DataSet.Insert to be called, so the AfterInsert event is definitely OK.
Best regards / Pozdrawiam
paweld

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
[SOLVED] DBGrid, set defaults when adding new Row
« Reply #4 on: September 16, 2025, 01:20:27 pm »
Add a record with DBNavigator causes DataSet.Insert to be called, so the AfterInsert event is definitely OK.
OK, i did try AfterInsert, but not to the end.
I just set a breakpoint in the Event, and it fired before the new row was painted.

Will report back

EDIT:
Reporting back.
I'm an idiot for not trying AfterInsert to the End.
Yepp. The Dataset is passed as the Argument. Setting FieldByName to the 4 Columns and setting the True/False for each one gives me exactly what i want.

Thx Pawel
« Last Edit: September 16, 2025, 01:24:03 pm 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

Sieben

  • Sr. Member
  • ****
  • Posts: 380
Re: [SOLVED] DBGrid, set defaults when adding new Row
« Reply #5 on: September 16, 2025, 03:41:47 pm »
For setting defaults, I would recommend OnNewRecord since it does not set the Modified-Flag as opposed to AfterInsert. The Modified-Flag in turn affects the way Cancel is handled.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: [SOLVED] DBGrid, set defaults when adding new Row
« Reply #6 on: September 16, 2025, 04:15:58 pm »
For setting defaults, I would recommend OnNewRecord since it does not set the Modified-Flag as opposed to AfterInsert. The Modified-Flag in turn affects the way Cancel is handled.
Hmm... OK
Thx for the Info.
Will have to keep an eye on it
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