Solved

Search in a TDBGRID

Posted on 1998-05-10
5
414 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

19 Experts available now in Live!

Get 1:1 Help Now