Working With TDBGrid - Search in Several Fields
Test and Monitor | Posted March 27, 2007

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 Locate method.
  • 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 SearchByLocateEx method.
  • 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.
Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox