//DbGrid的Column有个PickList属性相当于一个StringList,下面这个例子有测试通过
//Table1连到主表,Query1是个临时表,用一个TStringList存放ID字段
var
Form1 : TForm1;
implementation
var
lst_OrderNo : TStringList;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.Open;
lst_OrderNo := TStringList.Create;
with Query2 do
begin
Close;
SQL.Text := 'select ID,Name from Studnet order by ID';
OPen;
while not Eof do
begin
DBGrid1.Columns[0].PickList.Add(FieldByName('Name').AsString);
lst_OrderNo.Add(IntToStr(FieldByName('ID').AsString));
Next;
end;
Close;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
lst_OrderNo.Free;
Table1.Close;
end;
procedure TForm1.Query1BeforePost(DataSet: TDataSet);
begin
Table.FieldByName('ID').AsString := lst_OrderNo.Strings[DBGrid1.Columns[0].PickList.IndexOf(Table1.FieldByName('Name').AsString)];
end;