Link to home
Start Free TrialLog in
Avatar of sharol
sharol

asked on

Delete Records from DBGrid

how do I delete a record from a dbgrid which is connected to a dataset?
Avatar of drnadeem
drnadeem

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, Grids, DBGrids;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Table1: TTable;
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 If Key = VK_Delete then
 begin
    if MessageDlg('Are you sure you want to delete this record ?',
    mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    Table1.Delete;
    MessageDlg('Record has been deleted.', mtInformation,
      [mbOk], 0);

  end;
 end;
end;

end.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But please, prevent the users errors, adding this code in the beforeDelete event of your Ttable:

if Application.MessageBox('¿Are you sure....?','Atención',mb_OkCancel + mb_IconQuestion)= idcancel then
abort;

I think that dgConfirmDelete (Options) is set to False, to avoid the not customize message.

Best Regards.
Marcos.
sharol:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Hi!
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: kretzschmar

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

...Snehanshu
EE Cleanup Volunteer