Recent

Author Topic: How to Append a new line and save it in DBGrid?  (Read 13017 times)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: How to Append a new line and save it in DBGrid?
« Reply #15 on: August 01, 2018, 10:59:23 pm »
This one works perfectly!!!
Now, what if I want the record to be appended (that is to be added to the end of the rows) and not just inserted after the current?
(ok, there have been some suggestions that I should make my own button that calls append, but I was thinking of something more...elegant...like...subclassing the dbnavigator... :-X...what do you think?)
« Last Edit: August 01, 2018, 11:13:42 pm by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: How to Append a new line and save it in DBGrid?
« Reply #16 on: August 01, 2018, 11:57:47 pm »
No - just look at the DBNavigator events: There's an OnClick and it has the NavButton clicked as a parameter!

I tested this to work (hopefully, again...):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TDBNavButtonType);
  2. begin
  3.   if Button = nbInsert then
  4.     DBNavigator1.DataSource.Dataset.Append;
  5. end;

You should also consider sorting the query by the ID (SELECT ... FROM ... ORDER BY ...) then the new record will be put to the correct location automatically after posting.
« Last Edit: August 02, 2018, 12:05:08 am by wp »

egsuh

  • Hero Member
  • *****
  • Posts: 1289
Re: How to Append a new line and save it in DBGrid?
« Reply #17 on: August 02, 2018, 03:41:17 am »
Quote
I want the record to be appended

You may press down keys until a new blank row is created. But the position in database is meaningless. Well, might have had some meaning at dBASE or Paradox days, but not now. You'd better get accustomed  to using index, 'order by', etc.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to Append a new line and save it in DBGrid?
« Reply #18 on: August 02, 2018, 05:51:36 am »
This one works perfectly!!!
Now, what if I want the record to be appended (that is to be added to the end of the rows) and not just inserted after the current?
(ok, there have been some suggestions that I should make my own button that calls append, but I was thinking of something more...elegant...like...subclassing the dbnavigator... :-X...what do you think?)
it is far easier and faster to drop a toolbar wiht some tool buttons that emulate or even replicate the dbnavigator behavior than to subclass dbnavigator to add a button. So I personally wouldn't waste time on it unless you plan to
1) create a feature request in the bug tracker with the new control
2) create a component library or extend an existing component library

in which case I would suggest to either get intouch with the library authors before any decisions to see how they would like to proceed.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: How to Append a new line and save it in DBGrid?
« Reply #19 on: August 02, 2018, 08:13:45 am »
Use DBGrid for info and a new form with editfields for modifying / insert  reords.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: How to Append a new line and save it in DBGrid?
« Reply #20 on: August 02, 2018, 12:13:55 pm »
I think I finished it!
(check the attached file)

The essence of the changes I made is in the before post event of the Query2:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SQLQuery2BeforePost(DataSet: TDataSet);
  2. var SQLQuery1_country_id: Integer;
  3. begin
  4.   SQLQuery1_country_id := SQLQuery1.FieldByName('country_id').AsInteger;
  5.   if Dataset.FieldByname('country_id').IsNull then
  6.   begin
  7.     Dataset.FieldByname('country_id').Value:= SQLQuery1_country_id;
  8.   end;
  9. end;    
  10.  

I think it 's ok now, what do you think?
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: How to Append a new line and save it in DBGrid?
« Reply #21 on: August 02, 2018, 10:12:52 pm »
I see...silence, I 'll interprete this as an approval.
I think this project could be incuded in Lazarus' example projects.
It would have saved me a lot of time if it was there.
« Last Edit: August 02, 2018, 10:34:10 pm by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

 

TinyPortal © 2005-2018