Recent

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

panoss

  • Full Member
  • ***
  • Posts: 156
  • 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 7 32bit, Lazarus v1.8.4    FPC 3.0.4.

wp

  • Hero Member
  • *****
  • Posts: 5838
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 »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

egsuh

  • Full Member
  • ***
  • Posts: 189
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: 5363
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: 921
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 1.84 (32b) / FPC 3.0.4
Windows 10

panoss

  • Full Member
  • ***
  • Posts: 156
  • 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 7 32bit, Lazarus v1.8.4    FPC 3.0.4.

panoss

  • Full Member
  • ***
  • Posts: 156
  • 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 7 32bit, Lazarus v1.8.4    FPC 3.0.4.