Recent

Author Topic: Add,update and delete by dbgrid  (Read 497 times)

Gebo

  • Jr. Member
  • **
  • Posts: 52
    • chahpander for lingeries
Add,update and delete by dbgrid
« on: March 14, 2025, 08:24:22 pm »
please guys,I want any tutorials that explain how to add new record or edit through dbgrid
I like programming I begun by vb,vb.net,c# and finally free pascal and lazarus .I like martial arts sports and I am a kickboxer and made our team sport club called black eagle school I have a website for modern clothes..

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 345
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Add,update and delete by dbgrid
« Reply #1 on: March 14, 2025, 09:42:35 pm »
Probably need more information. What database are you using. Easy way is on your form put a panel at the top and then put a DBNavigator on the panel. Then below the panel put your DBGrid. Connect the Navigator and the Grid to a Dataset. The Dataset is pointing to your Database but all this depends on whether your Database is SQL or SQLite3. Or could be TDbf.
Start reading online a good start for SQLite is https://www.tweaking4all.com/software-development/lazarus-development/lazarus-pascal-getting-started-with-sqlite/ or this one for SQL:  https://wiki.freepascal.org/SQLdb_Tutorial1

Gebo

  • Jr. Member
  • **
  • Posts: 52
    • chahpander for lingeries
Re: Add,update and delete by dbgrid
« Reply #2 on: March 15, 2025, 08:47:39 am »
I use mariadb through odbc connection .
I want to create buttons myself ,I don't want to use dbnavigator.
I like programming I begun by vb,vb.net,c# and finally free pascal and lazarus .I like martial arts sports and I am a kickboxer and made our team sport club called black eagle school I have a website for modern clothes..

paweld

  • Hero Member
  • *****
  • Posts: 1351
Re: Add,update and delete by dbgrid
« Reply #3 on: March 15, 2025, 09:29:14 am »
to add a new record
Code: Pascal  [Select][+][-]
  1. DBGrid1.DataSource.DataSet.Insert;
to edit record
Code: Pascal  [Select][+][-]
  1. DBGrid1.DataSource.DataSet.Edit;
to delete a record
Code: Pascal  [Select][+][-]
  1. DBGrid1.DataSource.DataSet.Delete;

to confirm the add/edit
Code: Pascal  [Select][+][-]
  1. DBGrid1.DataSource.DataSet.Post;
to cancel the add/edit
Code: Pascal  [Select][+][-]
  1. DBGrid1.DataSource.DataSet.Cancel;

When approving and canceling changes, it's a good idea to make sure the dataset is in add/edit mode - the State property is used for this, e.g.
Code: Pascal  [Select][+][-]
  1. if not (DBGrid1.DataSource.DataSet.State in [dsInsert, dsEdit]) then
  2.   begin
  3.     ShowMessage('Dataset is not in insert/edit mode');
  4.     exit;
  5.   end;  
Best regards / Pozdrawiam
paweld

wcage03

  • New Member
  • *
  • Posts: 27
Re: Add,update and delete by dbgrid
« Reply #4 on: March 18, 2025, 07:42:11 pm »
For simple database scenarios, the DBGrid can pretty much handle the inserts and updates automatically. If you have the property AutoEdit turned on, then you can double click on a cell to start the editor, make your changes, move to another row and the changes will be saved. If you continue past the last row displayed, a row insert will occur allowing you to enter in the values needed. Again, moving to another row will automatically save your changes.

If the data table is more complex, you will need to add SQL to the InserSQL, UpdateSQL, and/or DeleteSQL properties. For example, if you are displaying data from a view in the grid, but you want. updates to be performed on the underlying tables, you can create your SQL so that is accommodated. Other things like auto incrementing values can be tricky and the requirements vary by the database.

You will also need to make sure that you have logic to ensure that any open transactions are recognized and saved if the form is exited and also decide how you want to handle transactions and commits. Since you are asking this question you are, I would recommend going the approach of automatically committing transactions. Remember to select the option to keep the dataset open after a commit also.

Good luck!

 

TinyPortal © 2005-2018