您可以使用 AfterScrollEvent 将 PickList 分配给您的列。
Picklist 是分配给主 StringList 对象的 Stringlists。取决于您的 Delphi 版本,您可以使用通用字典。
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, Grids, DBGrids, ADODB;
type
TForm2 = class(TForm)
ADODataSet1: TADODataSet;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
procedure FormCreate(Sender: TObject);
procedure ADODataSet1AfterScroll(DataSet: TDataSet);
private
{ Private-Deklarationen }
FList: TStringList;
public
{ Public-Deklarationen }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.ADODataSet1AfterScroll(DataSet: TDataSet);
var
idx: Integer;
begin
if Assigned(FList) and (DBGrid1.Columns.Count > 1) then
begin
DBGrid1.Columns[1].ButtonStyle := cbsAuto;
idx := FList.IndexOf(DBGrid1.Columns[0].Field.asString);
if idx > -1 then
DBGrid1.Columns[1].PickList := TStringList(FList.Objects[idx])
else
DBGrid1.Columns[1].PickList := nil;
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
var
i: Integer;
begin // some demo filling
FList := TStringList.Create;
FList.AddObject('A1A1', TStringList.Create);
for i := 0 to 10 do
TStringList(FList.Objects[FList.Count - 1]).Add(Format('group1_%d', [i]));
FList.AddObject('A1A2', TStringList.Create);
for i := 0 to 10 do
TStringList(FList.Objects[FList.Count - 1]).Add(Format('group2_%d', [i]));
end;
procedure TForm2.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for I := 0 to FList.Count - 1 do Flist.Objects[i].Free;
FList.Free;
end;
end.