Solved

Search in a TDBGRID

Posted on 1998-05-10
5
415 Views
Last Modified: 2010-08-05
How can I make a search in a TDBGRID ?
0
Comment
Question by:foure
5 Comments
 
LVL 4

Expert Comment

by:BoRiS
ID: 1344060
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
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1344061
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
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344062
Hi,

? Why not just searching the underlying dataset?

regards, ZIf.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1344063
Search in Delphi help on FindKey and Locate methods of TTable.
-this will be helpful.
0
 
LVL 3

Accepted Solution

by:
rickpet earned 50 total points
ID: 1344064
There are multiple ways to do this...Note DBGrid have a one to one relationship with their datasource.

The simplest...loop through your dataset looking for the value.  This is not very efficient as it doesn't take into account keys or index's.
i.e.
With DataSet do
begin
First;
while not eof then
begin
if DataSet.FieldByName('field1').value = MySearch then
  begin
    blFound := true;
    break;
  end;
Next;
end;
end;

or use Locate from TBDEDataSet
(*taken from Delphi help file*)
with CustTable do
Locate('Company;Contact;Phone', VarArrayOf(['Sight Diver', 'P']), [loPartialKey]);

or you could delimit your data in the dataset.  For Tables look at SetRange, for Queries look at Where statement's

Rick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now