tags:

views:

376

answers:

2

Hi,

is it possible to set a TDBGrid (or TwwDBGrid) cell ReadOnly in Delphi? Currently I am fiddling around with literally greying the cell and clearing it after an edit, but it's not very satisfactory.

Cheers, Jamie

+2  A: 

Specific cell or all cells in one column?

You may setup a column to read only in this way:

TDBGrid.Colums[IndexOfColumn].ReadOnly := True;

If you want to control a specific cell then you could try to program a "protection" scheme in the OnCellClickEvent. I guess you could even setup TDBGrid.Colums[IndexOfColumn].ReadOnly := True; in that event when a given cell should be read only. Something like:

procedure TForm.DBGridCellClick(Column: TColumn);
begin
  Column.ReadOnly := ConditionForReadOnly(Column);
end;

After edit:

I've checked this solution and it works.

For example, if you want to edit only cells in column greater then first and their value must be 0(for be able to edit them) then the protection scheme would look like this:

procedure TForm.DBGridCellClick(Column: TColumn);
begin
  Column.ReadOnly := (qry['FieldWithValue'] <> 0) or (Column.Index < 1);  //Index is 0-based
end;
Wodzu
Thanks, combined with OnDrawCell rendering grey that's perfect.
Jamie Kitson
A: 

As Wodzu has said, TColumn has a ReadOnly property.

_J_