我用DBGrid,ADODataSet和DataSourse三个组件读取数据库的一个表,数据读出来了,现在想实现一个查找功能。即点击窗体上的“查找”按钮,出现查找对话框,在查找对话框里输入内容,然后点击“开始查找”按钮进行查询,查询之后在DBGrid中用不同颜色高亮显示该记录所在行。

解决方案 »

  1.   

    要查找一条还是多条?只一条的话 可以使用ADODataSet.Locate来实现
      

  2.   

    改变DBGrid行颜色容易。
    如何动态改变TDBGrid中行的颜色,代码如下:procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
     if Table1.FieldByName('Salary').AsCurrency>36000 then
      DBGrid1.Canvas.Brush.Color:=clWhite;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;    上述代码执行的功能是:把薪水超过3万6千元的员工(employee)记录背景用白色(White)标出来。
      

  3.   

    查找功能,最好用ADODataSet.filter实现,参考Delphi的帮助文档。
      

  4.   

    DataSource1.DataSet.Locate('端口号码0',Edit1.text,[loPartialKey]);用这句实现了,查找,但是怎么让查找到的那行变颜色显示呢
      

  5.   

    不需要出现查找对话框,直接就在本DBGrid实现显示就可以了改变颜色procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
     if Table1.FieldByName('端口号码0').AsCurrency=IntToStr(Edit1.text) then
      DBGrid1.Canvas.Brush.Color:=clYellow;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;