Working With In-place Editors in Microsoft DataGridView

Before entering data into the Microsoft DataGridView cell, first you need to select it and activate its in-place editor, and after you modify the cell value, you need to save the changes. In my previous post, I explained how you can select cells in the Microsoft DataGridView control. In this post, I will explain how you can activate the cell’s editor to be able to change the cell value, access the editor object and close the editor saving the changes you have made.

Activating In-place Editors

In a Microsoft DataGridView control, selecting a cell does not necessarily activate the edit mode. The way the user can activate the cell’s editor depends on the grid’s EditMode property (you can determine the value of this property by exploring the grid object in TestComplete’s Object Browser):

  • If EditMode is EditOnEnter, the cell's edit mode is automatically activated when it is selected. There is no need to activate the cell’s editor.
  • If EditMode is anything except EditProgrammatically, the cell’s editor is activated when the user clicks the cell that has been previously focused. At that, the cell's contents are selected and the cursor is placed at the end of the selection.
  • If EditMode is EditOnF2 or EditOnKeystrokeOrF2, the selected cell's edit mode is activated upon pressing the F2 key. The cursor is placed at the end of the cell contents. To simulate the F2 shortcut, you can use the Keys action.
  • If EditMode is EditOnKeystroke or EditOnKeystrokeOrF2, the selected cell's edit mode is activated when the user starts typing in it. The cell's contents are replaced with the typed text.
  • For all EditMode values, it is possible to activate the cell’s editor using the grid’s BeginEdit method. This method has the selectAll parameter of the Boolean type that specifies whether the cell contents should be selected (if selectAll is True) or not (if selectAll is False). However, this approach does not imply any user interaction with the grid control, so it may not suit your needs.

The following ActivateCellEditor routine illustrates some of the described approaches that can be used to activate the cell’s editor. To simulate a click on the grid cell, I use the ClickCell routine from my previous blog.

Accessing In-place Editors

After the cell editor is activated, you can modify the cell value. In most cases, you can successfully do this by simulating the user input over the cell. However, sometimes you may need to perform these actions over the cell editor itself (for example, if the grid uses custom editors or complex dropdown editors for particular columns). You can access the cell editor object in one of the following ways:

  • Using native members of the Microsoft DataGridView control. The DataGridView.EditingControl property returns the object that serves as the cell editor, when the cell is in the edit mode. You can use native properties and methods of the obtained editor object to modify the cell value.
  • Using the WinFormsObject or WaitWinFormsObject method. In the application objects tree, some types of editing controls, for example, text box or combo box editors, appear as child objects of the grid control. Thus, you can refer them in scripts using the WinFormsObject or WaitWinFormsObject method, in the following way:

    grid.WinFormsObject("Panel", "").WinFormsObject(editor_class_name, "")

    where the WinFormsObject("Panel", "") statement refers the panel containing the editor control, and editor_class_name is the class name of the cell editor control. For standard text box cell editors this is “DataGridViewTextBoxEditingControl”, for standard combo box editors -- “DataGridViewComboBoxEditingControl”, and so on. You may find this way of acessing grid cell editor controls more convinient, because in this case TestComplete recognizes the editing controls as standard Win32 controls and extends their internal functionality with special properties and methods of the corresponding object (Win32TextBox, Win32ComboBox or the like). So, you can simulate various actions over the editing control by means of TestComplete.

For example, below is a sample routine that selects an item in the combo box cell. (I will write more on getting and setting cell values in one of the future posts.)

Closing In-place Editors

To finish editing the DataGridView cell value and commit any changes made, you should press the Enter key, or select another grid cell. You may also need to cancel the editing and discard any changes made to the cell -- this happens when the you press Esc in the grid. In scripts, you can simulate these keypresses using the Keys action.

The code snippet below contains two sample routines that can be used to commit and cancel editing. The Grid parameter of both routines specifies the grid control under which the action is performed:


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