Board index » delphi » Disable Insert in Grid on EOF

Disable Insert in Grid on EOF

Hello,

I have a form view and a grid view for my form's contents.  I only want the
user to add records in 'form view' so I make sure that the Insert keypress
switches to Form View to complete the entry.  My problem is if the user
using the down arrow key or the tab key on the table's last record, a new
record is automatically added.

How can I stop the auto-insert in grid view with disabling functionality in
form view or not allowing tab or the down arrow?

Thanks

 

Re:Disable Insert in Grid on EOF


It is probably best to set AutoEdit to false in your Datasource component.
This disables automatic edits or inserts in your Grid. This way YOU can
control when an edit or insert takes place. This is even neccesary if you
want to do data entry in a special form instead of in the Grid.

If for some reason you want to allow edits in your Grid but not inserts you
can add this code to your OnStateChange event of your Datasource component:

procedure TForm1.DataSource1StateChange(Sender: TObject);
begin
 if (DataSource1.State = dsInsert) and (ActiveControl=DBGrid1) then begin
    Datasource1.Dataset.Cancel;
 end;
end;

It checks if the State was changed to insert and if that event occurred when
your Grid was the Active control on your form. If that is the case it
cancels the insert operation.
This is in my opinion not the preferred method but it works.

Good luck,

Ben van Mierlo.

Other Threads