user4054093
user4054093

Reputation:

DBGrid set some cell as readonly

With dbgrid, we can set some Column as read only

DBGrid1.Columns[C].ReadOnly := True;

In my case, i need to set some cell (and NOT all cell in this Column ) as read only.

dbgrid

You can see in my image, if i have "No" value in field1 we should enable edit on field Tx. But if i have "Yes" value we should set Tx field as ReadOnly.

Is it possible to do that?

Upvotes: 0

Views: 5101

Answers (1)

MartynA
MartynA

Reputation: 30755

Yes, it is possible. The key to a solution to this is the fact that the dataset cursor of the dataset connected to the grid is synchronised to the current row in the grid, because of the way TDBGrid is coded.

So, all you need to do is to set up an AfterScroll event for the dataset and put your code to set the grid's ReadOnly property in that. In my case, the following works fine:

procedure TForm1.CDS1AfterScroll(DataSet: TDataSet);
begin
  DBGrid1.Columns[4].ReadOnly := Odd(CDS1.FieldByName('ID').AsInteger);
end;

Obviously, in your case you could test the contents of your Yes/No field.

If you want the grid to react immediately to a change in the value of the field that you are using to determine whether the column should be read-only, you should execute the same code in the dataset's AfterEdit event.

Upvotes: 8

Related Questions