Link to home
Start Free TrialLog in
Avatar of foure
foure

asked on

Search in a TDBGRID

How can I make a search in a TDBGRID ?
Avatar of BoRiS
BoRiS

here is a code snipit that I used to search a DBMemo maybe it will help you out
add a finddialog to your form...asign it to the onfind event ....


procedure TMainForm.FindDialog1Find(Sender: TObject);
var Buff, B, F : PChar;
    BuffL     : Word;
begin
   With Sender as TFindDialog do
   begin
      GetMem(F, Length(FindText) + 1);
      StrPCopy(F, FindText);
      BuffLen:= DBMemo1.GetTextLen + 1;
      GetMem(Buff,BuffL);
      DBMemo1.GetTextBuf(Buff,BuffL);
      B:= Buff + DBMemo1.SelStart + DBMemo1.SelLength;
      B:= StrPos(B, F);
      if B = NIL then MessageBeep(0)
      else
      begin
       DBMemo1.SelStart:= B - Buff;
       DBMemo1.SelLength:= Length(FindText);
      end;
   FreeMem(FT, Length(FindText) + 1);
   FreeMem(Buff,BuffL);
  end;
end;

To highlight the found text:

begin
       DBMemo1.SelStart:= B - Buff;
       DBMemo1.SelLength:= Length(FindText);
      end;
   FreeMem(F, Length(FindText) + 1);
   FreeMem(Buff,BuffL);
   DBMemo1.SetFocus;
  end;

If this helps let me know

Later
BoRiS
here is a code snipit that I used to search a DBMemo maybe it will help you out
add a finddialog to your form...asign it to the onfind event ....


procedure TMainForm.FindDialog1Find(Sender: TObject);
var Buff, B, F : PChar;
    BuffL     : Word;
begin
   With Sender as TFindDialog do
   begin
      GetMem(F, Length(FindText) + 1);
      StrPCopy(F, FindText);
      BuffLen:= DBMemo1.GetTextLen + 1;
      GetMem(Buff,BuffL);
      DBMemo1.GetTextBuf(Buff,BuffL);
      B:= Buff + DBMemo1.SelStart + DBMemo1.SelLength;
      B:= StrPos(B, F);
      if B = NIL then MessageBeep(0)
      else
      begin
       DBMemo1.SelStart:= B - Buff;
       DBMemo1.SelLength:= Length(FindText);
      end;
   FreeMem(FT, Length(FindText) + 1);
   FreeMem(Buff,BuffL);
  end;
end;

To highlight the found text:

begin
       DBMemo1.SelStart:= B - Buff;
       DBMemo1.SelLength:= Length(FindText);
      end;
   FreeMem(F, Length(FindText) + 1);
   FreeMem(Buff,BuffL);
   DBMemo1.SetFocus;
  end;

If this helps let me know

Later
BoRiS
Hi,

? Why not just searching the underlying dataset?

regards, ZIf.
Search in Delphi help on FindKey and Locate methods of TTable.
-this will be helpful.
ASKER CERTIFIED SOLUTION
Avatar of rickpet
rickpet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial