我的程序中有好多表,表中各一DBgrid1
如何在主窗体上写函数过程 把所要找的字段值添加到combobox1中
procedure Tmainform.chaxunzhi;
var
dbgrid1:Tdbgrid;
nowform:TForm;
ziduan:string;
begin
nowform.dbgrid1.DataSource.DataSet.First;
combobox1.Items.clear;
while not nowform.dbgrid1.DataSource.DataSet.eof do
begin
chaxunform.combobox1.Items.add(nowform.dbgrid1.DataSource.DataSet.FieldByName(ziduan).asstring);
nowform.dbgrid1.DataSource.DataSet.next;
end;
end;大体是这样写,不过不对
如何在主窗体上写函数过程 把所要找的字段值添加到combobox1中
procedure Tmainform.chaxunzhi;
var
dbgrid1:Tdbgrid;
nowform:TForm;
ziduan:string;
begin
nowform.dbgrid1.DataSource.DataSet.First;
combobox1.Items.clear;
while not nowform.dbgrid1.DataSource.DataSet.eof do
begin
chaxunform.combobox1.Items.add(nowform.dbgrid1.DataSource.DataSet.FieldByName(ziduan).asstring);
nowform.dbgrid1.DataSource.DataSet.next;
end;
end;大体是这样写,不过不对
begin
if not DataSet.Active then
DataSet.Active:=True;
DataSet.First;
Items.Clear;
while not DataSet.Eof do
begin
Items.Add(DataSet.FieldByName(FieldName).AsString);
DataSet.Next;
end;
end;调用方法:AddToCombobox(nowform.dbgrid1.DataSource.DataSet,ziduan,chaxunform.combobox1.Items);
否则太难看了,记录跑最后去了
mastersky(浪) 的代码能实现动态的字段,但动态的窗体怎么弄呀
既然DataSet、FieldName、Combobox的Items全部都是动态的,还有什么需要动态呢?
procedure AddToStrList(AAlias,TBName,FldName:string;StrList:TStrings;
const IsOrder:TOrderMode=omAsc;distinct:Boolean=True;const Condition:string='');
var
Query:TAdoQuery;
Sqlss:string;
begin
StrList.Clear;
Query:=TAdoQuery.Create(nil);
try
Query.ConnectionString:=AAlias;
if distinct then
Sqlss:=Format('Select Distinct %s From [%s]',[FldName,TBName])
else
Sqlss:=Format('Select %s From [%s]',[FldName,TBName]);
if Condition<>'' then
Sqlss:=Sqlss+' Where '+Condition;
case IsOrder of
omAsc:Sqlss:=Sqlss+Format(' order by %s Asc',[FldName]);
omdesc:Sqlss:=Sqlss+Format(' order by %s Desc',[FldName]);
omNone:Sqlss:=Sqlss;
end;
Query.SQL.Add(Sqlss);
Query.Open;
while not Query.Eof do
begin
StrList.Add(Query.Fields[0].AsString);
Query.Next;
end;
finally
Query.Free;
end;
end;
dataset为定值dbgrid1
fieldname 和当前窗体 是动态的
TOrderMode = (omNone,omAsc,omDesc);procedure AddToStrList(AAlias,TBName,FldName:string;StrList:TStrings;
const IsOrder:TOrderMode=omAsc;distinct:Boolean=True;const Condition:string='');
var
Query:TQuery;
Sqlss:string;
begin
StrList.Clear;
Query:=TQuery.Create(nil);
try
Query.DatabaseName:=AAlias;
if distinct then
Sqlss:=Format('Select Distinct %s From "%s"',[FldName,TBName])
else
Sqlss:=Format('Select %s From "%s"',[FldName,TBName]);
if Condition<>'' then
Sqlss:=Sqlss+' Where '+Condition;
case IsOrder of
omAsc:Sqlss:=Sqlss+Format(' order by %s Asc',[FldName]);
omdesc:Sqlss:=Sqlss+Format(' order by %s Desc',[FldName]);
omNone:Sqlss:=Sqlss;
end;
Query.SQL.Add(Sqlss);
Query.Open;
while not Query.Eof do
begin
StrList.Add(Query.Fields[0].AsString);
Query.Next;
end;
finally
Query.Free;
end;
end;一个是ADO的,一个是BDE的.表,条件,排序方式都有了.
items 是定值
dataset为定值dbgrid1
fieldname 和当前窗体 是动态的调用方法:AddToCombobox(nowform.dbgrid1.DataSource.DataSet,ziduan,chaxunform.combobox1.Items);NowForm设置成变量就OK了
nowform:='form1';系统说dbgrid1这块不行
不过没什么用,定义str不能实现动态窗体,你能想想和什么方法把调出当前找开窗体
来做成动态的吗?
我现在这样写了
procedure Tmainform.AddToCombobox(FieldName:string;Items:TStrings);
var str:TDataSet;
begin
if nowform='Tkhlyform1' then
str:=khlyform.dbgrid1.DataSource.DataSet
else if nowform='Tdypbform1' then
str:=dypbform.DBGrid1.DataSource.DataSet
else if nowform='Tdydzform1' then
str:=dydzform.DBGrid1.DataSource.DataSet
else if nowform='Tdaopiform1' then
str:=daopiform.DBGrid1.DataSource.DataSet;
...... if not str.Active then
str.Active:=True;
str.First;
Items.Clear;
while not str.Eof do
begin
Items.Add(str.FieldByName(dm.datamodule1.gy_table2.fieldbyname('ziduanmc1').asstring).AsString);
str.Next;
end;
end;
回复人: arlyn_2004(arlyn) ( ) 信誉:100
用DBcombobox1行吗?对呀 我怎么没想到 ,用它绑定数据呀
我想想
我试了怎么不行,nowform:string;
nowform:='form1';系统说dbgrid1这块不行
var
NowForm:TForm;
如果NowForm是字符串则用下面的语句来调用:
TForm(Application.FindFindComponent(NowForm)).DBGrid1......
明天要交活了,现在只能用你做的代码做成办动态的了
我现在这样写了
procedure Tmainform.AddToCombobox(FieldName:string;Items:TStrings);
var str:TDataSet;
begin
if nowform='Tkhlyform1' then
str:=khlyform.dbgrid1.DataSource.DataSet
else if nowform='Tdypbform1' then
str:=dypbform.DBGrid1.DataSource.DataSet
else if nowform='Tdydzform1' then
str:=dydzform.DBGrid1.DataSource.DataSet
else if nowform='Tdaopiform1' then
str:=daopiform.DBGrid1.DataSource.DataSet;
......str:=TForm(Application.FindFindComponent(NowForm)).DBGrid1.DataSource.DataSet;
就可以了。