我想要根据客户编号(edit1)和客户名称(edit2)来查询客户信息,
在edit1和edit2中输入内容后点击查询按钮(btnok)后,查询结果显示到DBGrid中相应的位置。
请问这应该怎么做?
最好有代码说明。以下是我的一些代码:
procedure Tcustcheck.btnokClick(Sender: TObject);
var
   i:integer;
begin
  if btnok.Caption='查询' then
  begin
    if Edit1.Text<>'' then
    begin
      with ADOQuery1 do
      begin
        SQL.Clear;
        SQL.Text:='select * from TPADFA where DFA001="'+edit1.Text+'" or DFA002 like ''%'+edit2.Text+'%''';
        Open;
        i:=recordcount;
      end;
      if i>0 then
      begin
        ADOQuery1.SQL.Clear;
        ADOQuery1.SQL.Text:='select * from TPADFA';
        DBGrid1.Columns.Items[0].FieldName:=edit1.Text;//这里有问题,我不知道该怎么写下去。
      end;
    end
    else
    begin
    Edit1.SetFocus;
    exit;
    end;
  end;end;

解决方案 »

  1.   

    你不会将DBGrid与DataSet绑定啊~
    真不知道你写的是什么~
      

  2.   

    procedure Tcustcheck.btnokClick(Sender: TObject); 
    var
      FStr:String;
    begin
      FStr:='';
      if Edit1.Text<>'' then
          FStr:='DFA001='''+Edit1.Text+'''';  if Edit2.Text<>'' then
      begin
        if  FStr='' then  
             FStr:='DFA002='''+Edit2.Text+''''
        else FStr:=FStr+' and DFA002='''+Edit2.Text+'''';
      end;  if FStr='' then  FStr:='select  *  from TPADFA'
        else   FStr:='select   *  from TPADFA where  ' +FStr;
      ADOQuery1.Close;
      ADOQuery1.SQL.Text:=FStr;
      ADOQuery1.Open;
    end;
      

  3.   

    connection与数据库关联
    dataset与connection关联
    datasource与dataset关联
    dbgrid与datasouce绑定
      

  4.   

    procedure Tcustcheck.btnokClick(Sender: TObject); 
    var 
       i:integer; 
    begin 
      if btnok.Caption='查询' then 
      begin 
        if Edit1.Text <>'' then 
        begin 
          with ADOQuery1 do 
          begin 
            SQL.Clear; 
            SQL.Text:='select * from TPADFA where DFA001="'+edit1.Text+'" or DFA002 like ''%'+edit2.Text+'%'''; 
            Open; 
            //i:=recordcount; 
          end; 
          {if i>0 then 
          begin 
            ADOQuery1.SQL.Clear; 
            ADOQuery1.SQL.Text:='select * from TPADFA'; 
            DBGrid1.Columns.Items[0].FieldName:=edit1.Text;//这里有问题,我不知道该怎么写下去。 
          end; }
        end 
        else 
        begin 
        Edit1.SetFocus; 
        exit; 
        end; 
      end; end;将DataSource1的dataset设为ADOQuery1,将DBGrid的datasource设为DataSource1就行了
      

  5.   

    if i>0 then 
          begin 
            ADOQuery1.SQL.Clear; 
            ADOQuery1.SQL.Text:='select * from TPADFA'; 
            DBGrid1.Columns.Items[0].FieldName:=edit1.Text;//这里有问题,我不知道该怎么写下去。 
          end; 
    这段没用吧 应该是控件属性的问题
      

  6.   

    我用2楼的方法做了,可还是不行
    它不能查询出来,也不能把查询的结果在下面的DBGrid上面显示出来
    说明一点: 我已经把DBGrid与数据库连接起来了
      

  7.   

    加一个datasource   dbgrid的datasource指定那个datasource就可以呀