2011. január 30., vasárnap

How to avoid direct input into a TDBGrid when there is a lookup list available


Problem/Question/Abstract:

In my TDBGrid, some columns have a picklist. Users can enter any value in these columns, not only values from the picklist. Does anybody know a simple way to prevent this? I would like either to suppress direct input and only allow selection of an existing item of the picklist, or check that input is in the picklist.

Answer:

procedure DBGridColExit(Sender: TObject);
var
  S: string;
begin
  with TDBGrid(Sender) do
  begin
    if (SelectedField = myfield) and
      (Columns[SelectedIndex].PickList.IndexOf(SelectedField.AsString) = -1) then
      with SelectedField do
      begin
        S := AsString;
        AsString := '';
        raise ExceptionCreate(S + ' is not a valid thingummy.' +
          #13'Choose a value from the drop-down list.');
      end;
  end;
end;

1 megjegyzés: