Dbgrideh can provide you with a more beautiful and feature-rich dbgird, such as flat display, odd and even line of different colors (zebra), quick Find, total lines, Grid printing, etc. In later versions of Ehlib, there is also a very powerful filtering feature that displays a filter row below the DBGrid header, where the user enters the query criteria in the corresponding column of the row, and Dbgrideh can automatically filter out the required data for you. The following figure shows the Dbgrideh after filtering is enabled:
In view of the previous Ehlib release, many people asked about the implementation of the Ehlib filtering function, and asked to provide an example, so I will briefly introduce the EHLIB filtering function implementation, the relevant code and executable files will be published to the Delphi box, please go there to search and download.
1, the filter to achieve the precondition: Uses Ehlibado, Ehlibbde, etc. ..
To implement Dbgrideh automatic filtering, you must first add a few related unit ehlib to your project, if you are using ADO connection database, please add Ehlibado in the uses list of any unit of the project, if using BDE, add Ehlibbde, other possible additions are: ehlibmte;
2, enable filtering, display filter line: DBGridEh.STFilter.Visible: = True
Dbgrideh the filter row is not displayed by default, setting Stfilter.visible to True filters the rows to be displayed.
3, select: Client or server-side filtering. DBGridEh.STFilter.Local = true| False
This is a major step. Client filtering is the filter function of query itself, in memory filtering, not to the server side to query, the advantage is simple to achieve, filtering speed, the disadvantage is only to obtain locally updated data, other users new and modified data can not be reflected in real time.
Dbgrideh server-side filtering is implemented by "piecing together" SQL statements. Before filtering, the SQL statement of the Dbgrideh bound Query must contain a filter tag specified by Dbgrideh, which defaults to/*filter*/, which must be at the beginning of any line of SQL, and when the user enters the filter value, Ehlib generates the filter condition, and uses this bar Replace the content after filtering the tag, and then send the new SQL statement to the database side to execute, the filtered data is obtained.
The specific type of filter should be used, according to the actual requirements of the selection.
4, the implementation of client filtering (local filtering): DBGridEh.STFilter.Local: = True
Client-side filtering implementation is simple, and easy to understand, set Local to True, filtering function is OK.
5, server-side filtering implementation: DBGridEh.STFilter.Local: = False
Server-side filtering need to set Local to False, and then the rest is SQL statements, SQL is generally the following style: Adoquery. Sql. Text: = ' SELECT * from Students WHERE ' + #13 + '/*filter*/1=1 ';
The above code-generated SQL is executed very correctly in a database such as SQL Server that supports/* comments, but in Access there is an error and the solution is simple, changing the Dbgrideh filter tag: Dbutilseh. Sqlfiltermarker: = ' (1>0) and ';
Then the SQL for Query should be the following: Adoquery. Sql. Text: = ' SELECT * from Students WHERE ' + #13 + ' (1>0) and 1=1 ';
Server-side filtering is OK, too.
Note: If you are using Delphi 2007, you may find that the server-side filtering function is still invalid when you complete the above settings, due to a bug in the Ehlib Dbutilseh unit, which causes this bug to be seen in this article: Ehlib 4.1 in Delp Hi 2007 cannot filter and sort the problem. (This sample code contains the modified Dbutilseh, please go to the Delphi box download)
6 . Adding the following code to the initialization section of the unit will automatically filter: Var
Inipropstorageman:tinipropstoragemaneh;
Initialization
Inipropstorageman: = Tinipropstoragemaneh.create (nil);
Inipropstorageman.inifilename: = Extractfiledir (paramstr (0)) + ' \demo1.ini ';
Setdefaultpropstoragemanager (Inipropstorageman);
Dbgridehcenter.filtereditcloseupapplyfilter: = True;
Deffontdata.name: = ' Microsoft Sans Serif ';
For detailed usage, please refer to the Demo under the Ehlib installation directory.