Mirror

Avoid direct input into a TDBGrid when there is a lookup list available (2) (Views: 101)


Problem/Question/Abstract:

Could somebody please tell me how I can make a column in a dbgrid only accept a value from a picklist without the user being able to type something into the cell manually?

Answer:

Override the Grid's protected KeyPress, like this:

procedure TPresPLRMask_DbGrid.KeyPress(var Key: Char);
var
  col: TColumn;
begin
  inherited;
  if SelectedIndex < 0 then
    exit;
  col := Columns[SelectedIndex];
  if (col.PickList.Count > 0) and (col.ButtonStyle = cbsAuto) then
  begin
    {no keys allowed except the TAB key (arrow keys down fire KeyPress,
    they don't need to be handled here)}
    if (Key <> #9) then
    begin
      Key := #0;
      Abort;
    end;
  end;
end;

<< Back to main page