Solved

Search in a TDBGRID

Posted on 1998-05-10
5
422 Views
Last Modified: 2010-08-05
How can I make a search in a TDBGRID ?
0
Comment
Question by:foure
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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