Board index » delphi » TDBGrid: preventing users to add records

TDBGrid: preventing users to add records

Hello!

I'm using a TDBGrid to display data.

How can I prevent users to add/insert using the down/insert key?

--
Stefan Johansson
ste...@programduon.se

 

Re:TDBGrid: preventing users to add records


I presume that you want users to be able to edit the data but not to be able
to add new records.

If so then you can add Abort() to the table's BeforeInsert event handler.
This will cause an exception to appear when running in the de{*word*81} but not
otherwise.

There may be a cleaner way, I'm not sure.

Re:TDBGrid: preventing users to add records


If you just want to display data, set the grid's ReadOnly property to True.

--jd

Quote
Stefan Johansson wrote in message <7b1ct2$8c...@forums.borland.com>...
>Hello!

>I'm using a TDBGrid to display data.

>How can I prevent users to add/insert using the down/insert key?

>--
>Stefan Johansson
>ste...@programduon.se

Re:TDBGrid: preventing users to add records


I found the solution for my problem by myself.
I want users to be able to edit the data in the grid and I don't want to
assign any events directly to the dataset.
Here's how I did it.

Assign the OnKeyDown event  for the grid component.

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:
TShiftState);
var
    d: TDataSet;
    g: TDBGrid;
begin
        g := (Sender as TDBGrid);
        d := g.DataSource.DataSet;

    case Key of
        VK_INSERT:
            Abort;
        VK_DOWN:
            begin
                d.Next;
                Abort;
            end;
        VK_TAB:
            if g.SelectedIndex = g.Columns.Count-1 then begin
                d.Next;
                g.SelectedIndex := 0;
                Abort;
            end;
    end;
end;

Stefan Johansson skrev i meddelandet <7b1ct2$8c...@forums.borland.com>...

Quote
>Hello!

>I'm using a TDBGrid to display data.

>How can I prevent users to add/insert using the down/insert key?

>--
>Stefan Johansson
>ste...@programduon.se

Re:TDBGrid: preventing users to add records


I found the solution for my problem by myself.
I want users to be able to edit the data in the grid and I don't want to
assign any events directly to the dataset.
Here's how I did it.

Assign the OnKeyDown event  for the grid component.

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:
TShiftState);
var
    d: TDataSet;
    g: TDBGrid;
begin
        g := (Sender as TDBGrid);
        d := g.DataSource.DataSet;

    case Key of
        VK_INSERT:
            Abort;
        VK_DOWN:
            begin
                d.Next;
                Abort;
            end;
        VK_TAB:
            if g.SelectedIndex = g.Columns.Count-1 then begin
                d.Next;
                g.SelectedIndex := 0;
                Abort;
            end;
    end;
end;

Stefan Johansson skrev i meddelandet <7b1ct2$8c...@forums.borland.com>...

Quote
>Hello!

>I'm using a TDBGrid to display data.

>How can I prevent users to add/insert using the down/insert key?

>--
>Stefan Johansson
>ste...@programduon.se

Re:TDBGrid: preventing users to add records


I prefer to call Abort() in the BeforeInsert() handler -- it's simpler and
more elegant.

Why don't you want to assign an event handler?

Re:TDBGrid: preventing users to add records


Stefan,
  You may set the DBGrids dgEditing property to False ( this is a set element
of the Options property)...

--
-Adam
Delphi Developer Support/Delphi WizOp

Due to my workload, personal email more than likely won't get a
reply.

Support options are detailed at http://www.inprise.com/devsupport/
I am unable to answer support questions via email at this time.

Quote
Stefan Johansson wrote:
> I found the solution for my problem by myself.
> I want users to be able to edit the data in the grid and I don't want to
> assign any events directly to the dataset.
> Here's how I did it.

> Assign the OnKeyDown event  for the grid component.

> procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:
> TShiftState);
> var
>     d: TDataSet;
>     g: TDBGrid;
> begin
>         g := (Sender as TDBGrid);
>         d := g.DataSource.DataSet;

>     case Key of
>         VK_INSERT:
>             Abort;
>         VK_DOWN:
>             begin
>                 d.Next;
>                 Abort;
>             end;
>         VK_TAB:
>             if g.SelectedIndex = g.Columns.Count-1 then begin
>                 d.Next;
>                 g.SelectedIndex := 0;
>                 Abort;
>             end;
>     end;
> end;

> Stefan Johansson skrev i meddelandet <7b1ct2$8c...@forums.borland.com>...
> >Hello!

> >I'm using a TDBGrid to display data.

> >How can I prevent users to add/insert using the down/insert key?

> >--
> >Stefan Johansson
> >ste...@programduon.se

Re:TDBGrid: preventing users to add records


The beforePost solution is the proper one.  You will test
if Table.State = dsInsert then abort;

Arrow

Quote
Stefan Johansson wrote in message <7b1ki8$9i...@forums.borland.com>...
>I found the solution for my problem by myself.
>I want users to be able to edit the data in the grid and I don't want to
>assign any events directly to the dataset.
>Here's how I did it.

>Assign the OnKeyDown event  for the grid component.

>procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:
>TShiftState);
>var
>    d: TDataSet;
>    g: TDBGrid;
>begin
>        g := (Sender as TDBGrid);
>        d := g.DataSource.DataSet;

>    case Key of
>        VK_INSERT:
>            Abort;
>        VK_DOWN:
>            begin
>                d.Next;
>                Abort;
>            end;
>        VK_TAB:
>            if g.SelectedIndex = g.Columns.Count-1 then begin
>                d.Next;
>                g.SelectedIndex := 0;
>                Abort;
>            end;
>    end;
>end;

>Stefan Johansson skrev i meddelandet <7b1ct2$8c...@forums.borland.com>...
>>Hello!

>>I'm using a TDBGrid to display data.

>>How can I prevent users to add/insert using the down/insert key?

>>--
>>Stefan Johansson
>>ste...@programduon.se

Re:TDBGrid: preventing users to add records


Hi Stefa!

On my site you can take a SMDBGrid. This component is able a lot of useful
and including to forbid addition and removal of records in the grid.

--
With best regards, Mike Shkolnik.
FIDO: 2:463/106.14
E-Mail: mshkol...@rs-ukraine.kiev.ua
        m...@woccu.freenet.kiev.ua
WEB: http://www.geocities.com/SiliconValley/Grid/3989

Stefan Johansson D??? ???Y?? <7b1ct2$8c...@forums.borland.com> ...

Quote
>Hello!

>I'm using a TDBGrid to display data.

>How can I prevent users to add/insert using the down/insert key?

>--
>Stefan Johansson
>ste...@programduon.se

Re:TDBGrid: preventing users to add records


I don't want to assign an event on the table, since I want to be able to add
records to the table programmatically.

Stefan

Re:TDBGrid: preventing users to add records


I don't want to assign an event on the table, since I want to be able to add
records to the table programmatically.

Stefan

Re:TDBGrid: preventing users to add records


The user must be able to edit data in some of the columns in the grid, but
is not allowed to add new records.

Re:TDBGrid: preventing users to add records


The user must be able to edit data in some of the columns in the grid, but
is not allowed to add new records.

Other Threads