Selecting Cells in Syncfusion GridControl

One of the actions that you will perform the most with a grid is selecting cells. This topic describes various approaches that can be used in TestComplete scripts to select a particular cell in Syncfusion GridControl controls. Note that before selecting a cell you should identify the row and column in which the desired cell resides. For example, you can search for the row by the value or text in a particular column. You can learn how to do this from my previous article.


This article describes various approaches that can be used to select a particular cell in the Developer Express XtraGrid control:



  • Simulating clicks on cells.

  • Simulating keyboard shortcuts.

  • Using internal members and properties of the GridControl object.



All these approaches assume that TestComplete has access to the internal methods, properties and objects of the GridControl object. That is, the .NET Open Application Support plug-in must be installed and enabled.



Simulating Clicks on Cells


You can select a particular GridControl by simulating a mouse click on it. To do this, use the Click action applied to the grid control. This action requires that you know the coordinates of the desired cell relative to the grid. You can determine them using the following instruction:


rect = GridObj.RangeInfoToRectangle ( GridObj.GridCellsRange.Cell( RowIndex, ColIndex ) )


Here, GridObj is the GridControl object, RowIndex and ColIndex are the zero-based indexes of the cell’s row and column. The resulting rect object is a .NET System.Drawing.Rectange object that holds the grid-relative coordinates of the desired cell.


Note that before simulating a click on the cell, you need to make sure that the cell is situated in the grid’s visible area. You can scroll the cell into the visible area using the grid’s ScrollCellInView method:


GridObj.ScrollCellInView_3 ( RowIndex, ColIndex )


The following example demonstrates how you can select GridControl cells by simulating a mouse click on them. This example works with the GridControlSort sample application that comes with Syncfusion Essential Studio library and resides in the following folder:


» <Syncfusion EssentialStudio>WindowsGrid.WindowsSamplesFeatureSamplesGridControlSort


The example contains the following routines:



  • Main is the “main” routine of he example. It obtains the GridControl object and simulates mouse clicks on some grid cells.
  • ClickCell simulates a mouse click on the specified grid cell. Before clicking the specified cell, the grid is scrolled to make the cell visible, if nesessary. The routine has three parameters:

    • Grid - The tested GridControl object.
    • RowIndex and ColIndex - The zero-based indexes of the cell’s row and column.








Simulating Keyboard Shortcuts


To navigate through the grid, you can also use various keyboard shortcuts. For example, pressing an arrow key selects the neighbor cell in the direction of the arrow, Ctrl+Home navigates to the grid’s upper-left cell, Ctrl+End navigates to the bottom-right cell, and so on.


To simulate a keyboard shortcut, use the Keys action applied to the grid control. The code snippet below demonstrates how to simulate the Ctrl+Home shortcut in order to select the grid’s upper left cell:






Using the GridControl.CurrentCell Property


The GridControl object has the CurrentCell property that provides access to the currently selected grid cell and lets you move the cell’s focus. To select a new grid cell, you can use various overload versions of the CurrentCell.MoveTo method. For example, the MoveTo_3(RowIndex, ColIndex) method will select the cell specified by the absolute row and column indexes. For more information on other MoveTo method versions, refer to the Essential Grid documentation.


Below is an example that demonstrates how you can use the GridControl.CurrentCell.MoveTo_3 method in scripts. The example works with the same GridControlSort application and contains the following routines:



  • Main is the “main” routine of he example. It obtains the GridControl object and calls SelectCell to select some grid cells.
  • SelectCell selects the specified grid cell. If the grid cannot be navigated to the specified cell, the error message is posted to the log. The routine has the following parameters:

    • Grid - The tested GridControl object.
    • RowIndex and ColIndex - The zero-based indexes of the cell’s row and column.








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