ReadOnly TDBGrid/TwwDBGrid Cell in Delphi?

随声附和 提交于 2019-12-11 12:55:40

问题


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


回答1:


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;



回答2:


As Wodzu has said, TColumn has a ReadOnly property.



来源:https://stackoverflow.com/questions/2005491/readonly-tdbgrid-twwdbgrid-cell-in-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!