Rudolf Smith
Rudolf Smith

Reputation: 11

Deleting a entry in a DBGrid with a button in Delphi

I'm relatively new to delphi and I would like to know how to delete an entry in a dbGrid without using a dbNavigator but a button. The number of the entry that should be deleted must be entered in a spinedit, not be clicked on in the dbGrid. Thanks for any help.

Upvotes: 0

Views: 4898

Answers (1)

João Rocha
João Rocha

Reputation: 135

First it's nice to position in the first record of the DataSet, then it will delete from the first to the N record.

DBGrid1.DataSource.DataSet.First;


Now you create the Loop (Don't forget to create the variable {var I : integer})

For I:=0 to SpinEdit1.Value-1 Do


Before start deleting records, you will need to verify if there is any record on DataSet.
You can do something like this:

if DBGrid1.DataSource.DataSet.RecordCount > 0 then


And finally you can delete the record

DBGrid1.DataSource.DataSet.Delete;



The final code would be like this:

DBGrid1.DataSource.DataSet.First; //Set on the first Record of the DataSet
For I:=0 to SpinEdit1.Value-1 Do //Do loop
   if DBGrid1.DataSource.DataSet.RecordCount > 0 then //Check if have records
       DBGrid1.DataSource.DataSet.Delete; //Delete

Upvotes: 2

Related Questions