1

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.

3
  • 4
    You should perform the action on the dataset and not on the grid - thats why the navigator is also linked to the dataset/datasource and not to the grid. You can locate the row and then delete the matched row TDataSet.Locate TDataSet.Delete Commented Sep 27, 2014 at 22:09
  • depending on your dataset type (ADO/ClientDataset/?) this post might help. Commented Sep 28, 2014 at 13:38
  • 1
    Why, oh why, do you want to make life harder for yourself and your users? Unless you have a really good reason not to use a button (be it a navigator or otherwise), I would stick to what users can understand intuitively (a delete button deletes the record). Commented Sep 29, 2014 at 17:56

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.