我用DBgrid设置了字段的caption,怎么与数据集关联啊,我这样写代码怎么不显示数据,请高手赐教
procedure Tmain.cxButton1Click(Sender: TObject);
begin
if (trim(cxtextedit1.Text)='')and (trim(cxtextedit2.Text)='')and
(trim(cxtextedit3.Text)='') and (trim(cxtextedit4.Text)='')and
(trim(cxtextedit5.Text)='') then
begin
application.MessageBox('请输入查询条件','提示',64);
cxtextedit1.SelectAll ;
cxtextedit1.SetFocus ;
exit;
end;
with f_data.ADOQuery1 do
begin
close;
sql.Clear ;
sql.Add('select itemid,itemname,itemspecs,itemunit,category,memo');
sql.Add('from itemaccoun where itemid like :a ') ;
sql.Add('and itemname like :b ') ;
sql.Add('and itemspecs like :c') ;
sql.Add('and memo like :d');
sql.Add('and category like :e ') ; parameters.ParamByName('a').Value := '%'+trim(cxtextedit1.Text)+'%' ;
parameters.ParamByName('b').Value := '%'+trim(cxtextedit2.Text)+'%';
parameters.ParamByName('c').Value := '%'+trim (cxtextedit3.Text)+'%';
parameters.ParamByName('d').Value := '%'+trim(cxtextedit4.Text)+ '%';
parameters.ParamByName('e').Value := '%'+trim(cxtextedit5.Text)+'%';
open; end;
dbgrid1.Columns[0].FieldName :=f_data.ADOQuery1.fieldbyname('itemid').asstring ;
dbgrid1.Columns[1].FieldName :=f_data.ADOQuery1.fieldbyname('itemname').asstring ;
dbgrid1.Columns[2].FieldName :=f_data.ADOQuery1.fieldbyname('itemspecs').asstring ;
dbgrid1.Columns[3].FieldName :=f_data.ADOQuery1.fieldbyname('itemunit').asstring ;
dbgrid1.Columns[4].FieldName :=f_data.ADOQuery1.fieldbyname('category').asstring ;
dbgrid1.Columns[5].FieldName :=f_data.ADOQuery1.fieldbyname('memo').asstring ;end;end.
procedure Tmain.cxButton1Click(Sender: TObject);
begin
if (trim(cxtextedit1.Text)='')and (trim(cxtextedit2.Text)='')and
(trim(cxtextedit3.Text)='') and (trim(cxtextedit4.Text)='')and
(trim(cxtextedit5.Text)='') then
begin
application.MessageBox('请输入查询条件','提示',64);
cxtextedit1.SelectAll ;
cxtextedit1.SetFocus ;
exit;
end;
with f_data.ADOQuery1 do
begin
close;
sql.Clear ;
sql.Add('select itemid,itemname,itemspecs,itemunit,category,memo');
sql.Add('from itemaccoun where itemid like :a ') ;
sql.Add('and itemname like :b ') ;
sql.Add('and itemspecs like :c') ;
sql.Add('and memo like :d');
sql.Add('and category like :e ') ; parameters.ParamByName('a').Value := '%'+trim(cxtextedit1.Text)+'%' ;
parameters.ParamByName('b').Value := '%'+trim(cxtextedit2.Text)+'%';
parameters.ParamByName('c').Value := '%'+trim (cxtextedit3.Text)+'%';
parameters.ParamByName('d').Value := '%'+trim(cxtextedit4.Text)+ '%';
parameters.ParamByName('e').Value := '%'+trim(cxtextedit5.Text)+'%';
open; end;
dbgrid1.Columns[0].FieldName :=f_data.ADOQuery1.fieldbyname('itemid').asstring ;
dbgrid1.Columns[1].FieldName :=f_data.ADOQuery1.fieldbyname('itemname').asstring ;
dbgrid1.Columns[2].FieldName :=f_data.ADOQuery1.fieldbyname('itemspecs').asstring ;
dbgrid1.Columns[3].FieldName :=f_data.ADOQuery1.fieldbyname('itemunit').asstring ;
dbgrid1.Columns[4].FieldName :=f_data.ADOQuery1.fieldbyname('category').asstring ;
dbgrid1.Columns[5].FieldName :=f_data.ADOQuery1.fieldbyname('memo').asstring ;end;end.
dbgrid1.Columns[0].FieldName :=f_data.ADOQuery1.fieldbyname('itemid').asstring ;
dbgrid1.Columns[1].FieldName :=f_data.ADOQuery1.fieldbyname('itemname').asstring ;
dbgrid1.Columns[2].FieldName :=f_data.ADOQuery1.fieldbyname('itemspecs').asstring ;
dbgrid1.Columns[3].FieldName :=f_data.ADOQuery1.fieldbyname('itemunit').asstring ;
dbgrid1.Columns[4].FieldName :=f_data.ADOQuery1.fieldbyname('category').asstring ;
dbgrid1.Columns[5].FieldName :=f_data.ADOQuery1.fieldbyname('memo').asstring ;
这段还需要吗? 我猜你的意思 可能是你是想在dbgrdi中只显示你查询的数据 而不是所有的对不 若是这样的话 你先放一个adoquery1 数据集 然后在属性SQL 写上你的表 如果是多张表的话 请先在查询分析器中先做好 看能否查到数据 然后把他复制到SQL属性里,最后双击dbgrid打开的窗口中点填加项按钮然后就在属性fieldName中选择你想显示的字段在dbgrid中 最后你在查询按钮 我在窗体onshow 事件 就可以加上你上面的语句进行条件查询了,
例如下面的代码 上面那段就不要了,忘了 还要加个datebase组件哟 with f_data.ADOQuery1 do
begin
close;
sql.Clear ;
sql.Add('select itemid,itemname,itemspecs,itemunit,category,memo');
sql.Add('from itemaccoun where itemid =:a and itemname=:b') ;
sql.Add('and itemspecs=:c') ;
sql.Add('and memo =:d');
sql.Add('and category =:e ') ; parameters.ParamByName('a').Value :=like ''%'+trim(cxtextedit1.Text)+'%'' ;
parameters.ParamByName('b').Value := like''%'+trim(cxtextedit2.Text)+'%'';
parameters.ParamByName('c').Value := like''%'+trim (cxtextedit3.Text)+'%'';
parameters.ParamByName('d').Value := like''%'+trim(cxtextedit4.Text)+ '%'';
parameters.ParamByName('e').Value := like''%'+trim(cxtextedit5.Text)+'%'';
open;
end;
标题,宽度等,然后打开窗体时显示标题,但不显示记录,再执行查询按钮时把数据集中的内容显示在DBGRID中,然后
用DBGRID的标题对应数据集中的字段,让他们关联起来,也就是说标题名称显示数据集中的名称字段。
在你查询按钮执行不就可以了吗
数据集内容,内容都是空的,当点击查询BUTTON后再显示内容,而且数据集查询出来的内容字段要显示在下面对应的列中,我就是不知道如何处理例:(dbgrid)
名称 规格 单位 备注
newColumn: TColumn;
begin
...
newColumn := DBGrid.Columns.Add;
newColumn.Title.Alignment := taCenter; //对齐方式
newColumn.Title.Caption := '标题'; //动态显示的标题
newColumn.FieldName := fieldName; //需要动态绑定记录集/数据库表字段
newColumn.Width := displayWidth; //列宽度
...说明:你可以动态从数据库字典表(用户定义用于存储各表的字段信息,包括数据库字段名称、显示名称、显示控制、网格表现形式,如下拉框还是有个小button),然后动态创建表头。
名称 规格 单位 备注
添加方法如下:Add New添加一个0-TColumn,选中0-TColumn,在属性栏里面找到FieldName这个属性,填上在数据库中的字段名,如:名称这个字段在数据表中用name表示就写name,然后在Title属性下找到Caption,填上要在dbgrid中显示的中文字段这样创建完成以后,再在procedure Tmain.cxButton1Click(Sender: TObject); 事件下添加Sql语句即可创建永久字段的方法可能本人表达不是很清楚,请自己琢磨