DBGrid 筆記2
下拉式選單維護
方法1.偷懶方式 單欄位屬性PickList 開起來改
缺點維護麻煩
方法2 此方法透過Stringlist 資料帶過去
再透過位置指定儲存格
缺點如果欄位異動位置 Pos 需要更改
procedure TGAS22.load_param(item: string; pos: integer);
var s: tstringlist;
i:Integer;
begin
clientdataset5.Close;
clientdataset5.CommandText := format('SELECT * FROM PARAM_POOL WHERE PARAM_CATEGORY = ''%S'' AND PARAM_NAME=''%S'' ORDER BY SQUENCY', ['GAS22', item]);
clientdataset5.Open;
s := tstringlist.Create;
try
while not clientdataset5.Eof do
begin
s.Append(clientdataset5.fieldbyname('PARAM_VALUE1').AsString);
clientdataset5.Next;
end;
Dbgrid2.Columns[pos].PickList := s;
finally
s.Free;
end;
//使用方式
load_param('Machine', 8);
方法3 透過dbgrid field的名稱去做控管
好處是當該欄位前面被插入新欄位時不需去維護也可續RUN
procedure TGAS22.load_param(item: string; pos: integer);
var s: tstringlist;
i:Integer;
begin
clientdataset5.Close;
clientdataset5.CommandText := format('SELECT * FROM PARAM_POOL WHERE PARAM_CATEGORY = ''%S'' AND PARAM_NAME=''%S'' ORDER BY SQUENCY', ['GAS22', item]);
clientdataset5.Open;
s := tstringlist.Create;
try
while not clientdataset5.Eof do
begin
s.Append(clientdataset5.fieldbyname('PARAM_VALUE1').AsString);
clientdataset5.Next;
end;
for i:=0 TO Dbgrid2.Columns.Count-1 do
begin
if Dbgrid2.columns[i].field.Name='clientdataset2'+item then
begin
Dbgrid2.columns[i].picklist.clear;
Dbgrid2.Columns[i].PickList := s;
end;
end;
finally
s.Free;
end;
//使用方式
load_param('Machine', 8);
大家一起加入blogads 賺零用錢!!