2010. november 9., kedd

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 invalid.' +
          #13'Choose a value from the drop-down list.');
      end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése