DBLookupComboBox1 在 DBGrid1 中制作下拉

DBLookupComboBox1 在 DBGrid1 中制作下拉

表a的数据做为下拉内容(后台表),表b做为目的表(及要操作表)

 

组件:query1  DataSource1 query2  DataSource2 DBLookupComboBox1 DBGrid1  button1

用query1连接后台表a,用query2连接表b, DBGrid1连接表b

 

在query2的sql中写:select * from b  并 query2.Active:=true;

在query1的sql中写:select * from a  并 query1.Active:=true;

在DBGrid1的DrawColumnCell事件件中写

begin

if (gdfocused in state) then

begin

dbgrid1.Canvas.Brush.Color :=clred;

if (column.FieldName = '字段名' ) then  // DBLookupComboBox1对就的字段名

begin

DBLookupComboBox1.Left := Rect.Left + DBGrid1.Left;

DBLookupComboBox1.Top := Rect.Top + DBGrid1.top;

DBLookupComboBox1.Width := Rect.Right - Rect.Left;

DBLookupComboBox1.Height := Rect.Bottom - Rect.Top;

DBLookupComboBox1.Visible := True;

End //此处加“;”并去掉下列代码,为一个DBLookupComboBox1,这里是表示二个DBLookupComboBox组件;

else

if (column.FieldName = '字段名' ) then  // DBLookupComboBox2对就的字段名

begin

DBLookupComboBox2.Left := Rect.Left + DBGrid1.Left;

DBLookupComboBox2.Top := Rect.Top + DBGrid1.top;

DBLookupComboBox2.Width := Rect.Right - Rect.Left;

DBLookupComboBox2.Height := Rect.Bottom - Rect.Top;

DBLookupComboBox2.Visible := True;

DBLookupComboBox2.Visible :=true;

DBLookupComboBox1.Visible :=false;

end;

end;

end;

 

设置DBLookupComboBox1的属性:

LestSsource:= DataSource1;//这里连接的是后台表,及表a

LisField:=字段名称;

KeyField:=字段名称;//和LisField必须是一个字段名称

DataSource:= DataSource2;//这里连接的是前台操作表b

DataField:=字段名称; //这里的字段为要操作的字段

 

在button1中写如下代码:

begin

with query1 do

begin

sql.Clear ;  //清理sql

sql.Add('select distinct 字段名 from a '); //进行不重复查询

open; //打开数据库

end;

end;

试运行后,点button1.在再dbgrid1中单击定点的字段。

原文地址:https://www.cnblogs.com/chenbg2001/p/1604985.html