Working With TDBGrid - Search in Several Fields
To simulate user actions over the TDBGrid control or to obtain the data of the grid cells, you will have to locate the row that contains the data that you are going to work with. In my previous article I described the searching approach that uses the
Locate method of the TDataset object, whose data the grid displays. That article explained how to use this method to search in one field.
You can also use the
Locate method to search for a record by using several field values. To do this, you have to create arrays that will contain the sought-for values. Also, you should form the string that includes the desired fields' names separated by semicolons. The array and the string are then passed to the
Locate method as parameters. Below is a sample code that demonstrates this.
The sample contains the following routines:
SeachByLocateEx - Calls the dataset’s
GetFieldName - A helper routine that returns the name of the field that corresponds to a column.
GetFieldNameEx - A helper routine that processes the string holding the column names and returns the string containing the appropriate field names.
Main - The “main“ routine of the sample. It prepares an array of values to be used for searching and pass this array and the string of the appropriate column names to the
- JScript, C++Script and C#Script samples also use the
ConvertJScriptArray function. It is used to convert the
Values array to the format compatible with the
Locate function. The problem is that the JScript engine uses the arrays format that differs from the format used by the Borland VCL library. So, we have to call the
ConvertJScriptArray function to convert the
Values array to the appropriate format.