Working With TDBGrid - Iterating Through Grid Rows

Iterating through grid rows means accessing rows in a series, one by one. You may need to iterate through TDBGrid rows if you need to locate a particular row, or perform the same set of operations on each row.


To iterate through grid rows, you can either simulate the keystrokes on the grid window (UP ARROW, DOWN ARROW and others) or use methods and properties of the TDataset object, whose data the grid displays. In general, using the methods and properties of the dataset object is preferable, since, if the TDBGrid control supports multiple row selection, simulating keystrokes will remove the selection from the selected rows.


The methods of the TDataset object changes the active record in the dataset and when this happens, the TDBGrid control automatically selects the appropriate grid row. To iterate through grid rows you can use the following dataset methods and properties:



  • RecordCount - Returns the number of records in the dataset. If the dataset is filtered, the property will return the number of records that match the filter.
  • First - Activates the first record in the dataset.
  • Last - Activates the last record in the dataset.
  • Next - Activates the next record in the dataset.
  • Prior - Activates the previous record in the dataset.
  • MoveBy (Distance) - Activates the record that resides a specified number of records away from the current record. Positive value of the Distance parameter means forward movement. Negative value means backward movement.
  • BOF - Returns True if the first record of the dataset is selected.
  • EOF - Returns True if the last record of the dataset is selected.

Note: in order for TestComplete to be able to access these methods and properties, the application under test must be compiled as an Open Application with debug information.


Below is a script sample that demonstrates how you can iterate through the TDBGrid rows using methods and properties of the TDataset object.


The sample contains the following routines:



  • Main - The “main&Rdquo; routine of the sample. It obtains the scripting object that corresponds to the tested TDBGrid control and then passes this object and the desired file name to the IteratingRows routine, which performs the export of grid data to an XML file.
  • IteratingRows - This routine goes through grid rows and exports grid data to the specified XML file.

    To write data to a file, we use the FileSystemObject OLE object. You can find the object reference in the MSDN library. The on-line version is available at http://msdn.microsoft.com.


    To create an XML file, we use the simplest approach: we write tag names as text strings.


    To iterate through grid rows, the routine performs the following actions:



    • Calls the First method of the TDataset object, whose data the grid displays. This method activates the first record in the dataset.
    • Goes through the dataset records in a loop (when another record in the dataset is activated, the appropriate row in the grid is activated).
    • Within the loop body, the script code processes the fields of the current record. For each column the sample routine obtains the TField object that corresponds to the column and then uses the AsString property of this object to obtain the field value.

      Then, the script passes the field value to the ConvertSpecialChars function that replaces special characters such as &, > or < with the appropriate constant value (&amp;, &gt;, &lt; and others). This is necessary for the XML to be valid.


      After the special characters are replaced, the routine saves the resultant string to a file.


      To proceed with the next record, the routine calls the Next method of the TDataset object.


      The loop is over when the dataset’s EOF property returns 1. The EOF property signals if the last dataset record is active by returning True, otherwise it returns False. TestComplete gets the property value through the Debug Info Agent™. Due to certain peculiarities, you should convert the property value to the boolean type by calling the BuiltIn.VarToBool function provided by TestComplete (when calling the function, you can omit the object name, BuiltIn).


  • ConvertSpecialChars - Helper rotuine. It replaces special characters (&, <, > and others) in a string with their appropriate constants (&amp;, &lt;, &gt; and so on) and then returns the resulting string.


Close

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

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