简体   繁体   English

添加搜索以搜索DBGrid中的数据并临时更改DBGrid显示的内容-Delphi

[英]Adding a search that searches data in a DBGrid and temporarily changes what that DBGrid displays- Delphi

Ok so, I'm using a DBGrid that displays data from a database. 好的,我正在使用显示数据库中数据的DBGrid。

The database contains a table which contains 11 columns. 该数据库包含一个表,该表包含11列。 Column number 4 is called customer name and contains name of customers. 第4列称为客户名称,其中包含客户名称。

I'd like a search box (similar to google search bar) where I would write in a client name I want Once I do that the DBGrid should change and display only the rows that contain that client name in the 4th column (column customer names) 我想要一个搜索框(类似于google搜索栏),在其中输入我想要的客户名称。一旦这样做,DBGrid应该更改并仅在第四列中显示包含该客户名称的行(客户名称列) )

Example: 例:

Column names: 

column1 - column2 - column3 - customer name - column5 - column6 - column7 - column8 - column9 - column10 - column11
blabla    blabla    blabla    John Edwards    blabla    blabla    blabla    blabla    blabla    blabla     blabla
blabla1   blabla1   blabla1   Michael Skunk   blabla    blabla    blabla    blabla    blabla    blabla     blabla
blabla2   blabla2   blabla2   John Edwards    blabla2   blabla2   blabla2   blabla2   blabla2   blabla2    blabla2

And so on.. lots of rows, all with different information in every column. 依此类推..很多行,每一列都有不同的信息。 At some point I will have in my database different information in the rest of the columns but the same client name in column number 4 (customer name column) 在某些时候,我在数据库的其余各列中将具有不同的信息,但在第4列(客户名称列)中将具有相同的客户端名称

In my example, I already do, as you can see I have 2 entries for John Edwards 在我的示例中,我已经这样做了,如您所见,我有2个John Edwards条目

Subsequently when I write that customer name in the search field I'd like all those rows to display on the DBGrid while any other row that doesn't contain that name in column number 4 to dissapear 随后,当我在搜索字段中输入该客户名称时,我希望所有这些行都显示在DBGrid上,而其他任何在第4列中不包含该名称的行都将消失

If using the above example, once I write John Edwards in the search field I should only see those 2 rows on the DBGrid 如果使用上面的示例,一旦我在搜索字段中写了John Edwards,我应该只在DBGrid上看到那两行

Once I clear the search field of what I wrote, the DBGrid should revert back to it's initial state and display everything, like it was before searching. 一旦清除了我写的内容的搜索字段,DBGrid应该恢复到其初始状态并显示所有内容,就像在搜索之前一样。

Any idea how to do this? 任何想法如何做到这一点? I have no clue since I'm rather new to Delphi and searches both here and on Google haven't come up with anything useful / anything I can work with.. 由于我不熟悉Delphi,并且在这里和Google上进行搜索,因此我一无所知。我没有提出任何有用的信息/我可以使用的任何信息。

I would appreciate any help, thanks a lot! 我将不胜感激,非常感谢!

If you're dealing with a small number of rows returned by your SQL SELECT, you can use TDataSet.Filter and TDataSet.Filtered . 如果要处理SQL SELECT返回的少量行,则可以使用TDataSet.FilterTDataSet.Filtered You can get the input from anywhere you'd like, such as a plain old TEdit . 您可以从任何地方获取输入,例如普通的TEdit

As you've posted no details (such as the DB controls you're using, the version of Delphi, any code that gives variable names, or anything else), here's a very generic sample that may help. 由于您没有发布任何详细信息(例如您正在使用的数据库控件,Delphi的版本,提供变量名称的任何代码或其他任何内容),因此,这是一个非常有用的示例。 I'm calling the query attached to the DBGrid Qry , because have no idea what else to call it based on what you've posted. 我称该查询附加在DBGrid Qry ,因为不知道根据您发布的内容还要调用什么。 FilterRecordsButton and ClearFilterButton are TButtons, and SearchEdit is a TEdit . FilterRecordsButtonClearFilterButtonSearchEdit ,而SearchEditTEdit Feel free to use any control you want to toggle the filter or get the input from the user. 随意使用您想要切换过滤器或从用户那里获取输入的任何控件。

procedure TForm1.FilterRecordsButtonClick(Sender: TObject);
begin
  if SearchEdit.Text <> '' then
  begin
    {
     The brackets around the column name are required because you've got 
     spaces in the name; they're also needed if your column name is a 
     reserved word. QuotedStr puts the necessary quote characters around
     the value.
    }
    Qry.Filter := '[Customer Name] = ' + QuotedStr(SearchEdit.Text);
    Qry.Filtered := True;
    Qry.First;
    FilterRecordsButton.Enabled := False;
    ClearFilterButton.Enabled := True;
  end;
end;

procedure TForm1.ClearFilterButtonClick(Sender: TObject);
begin
  Qry.Filtered := False;
  Qry.Filter := '';
  Qry.First;
  ClearFilterButton.Enabled := False;
  FilterRecordsButton.Enabled := True;
end;

If you're dealing with a large number of rows ( SELECT * FROM MyTable without a WHERE that returns a few hundred thousand rows, for instance), then the performance if Filtered may not be acceptable. 如果要处理大量的行(例如, SELECT * FROM MyTable而没有返回几十万行的WHERE ),那么“已Filtered的性能可能是不可接受的。 In that case, you're better off just adding the appropriate WHERE clause to your SELECT and re-opening the query to display only the relevant rows. 在这种情况下,最好将适当的WHERE子句添加到SELECT然后重新打开查询以仅显示相关行。 Of course, you should never be doing a SELECT without a WHERE , so you won't need to do that. 当然,在没有WHERE情况下,您绝不应该执行SELECT ,因此您不需要这样做。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM