Getting and Setting DataGridView Cell Values
Test and Monitor | Posted February 16, 2007

Microsoft DataGridView cells can be of different types -- text boxes, combo boxes, check boxes, buttons, images, links, as well as custom types. Depending on the cell type, you can get or set cell values in different ways. Note that before changing the cell value, you need to select the needed grid cell and activate the edit mode, and after you make changes, you need to save them. You can learn how to select cell values and work with in-place editors in my previous posts:

Note: In the examples below, I use the following helper routines from the previous posts: GetCell, ClickCell, ActivateCell Editor, CloseCellEditor. So, if you are going to try out the examples, do not forget to include the routines in your script.

Getting Cell Values

You can get the value stored in the grid cell using the cell’s Value property. One more property, FormattedValue, lets you get the displayed representation of the cell value. Note that the .NET String, Decimal and DateTime objects are not OLE-compatible and you cannot use them directly in your scripts. To obtain the OLE-compatible values of these objects, you should use the OleValue property added to them by TestComplete.

The following example contains the sample GetCellValue routine that can be used to obtain the DataGridView cell text:

Setting Cell Values

The way you interact with Microsoft DataGridView cells and modify their values depends on the cell type, that is, you type in text boxes, check check boxes and select combo box items. However, it’s possible to distinguish three common approaches to modifying the cell value:

  1. Using the DataGridViewCell.Value property.
  2. Simulating actions over the cell’s in-place editor.
  3. Using the cell’s editor internal properties and methods.

1) Using the DataGridViewCell.Value property is the easiest way to change the value of any cell. Below is a sample SetCellValue routine that can be used for this purpose:

2) Simulating actions over the cell’s in-place editor. In the Working With In-place Editors in Microsoft DataGridView post, I explained how you can access the cell editor controls. Since TestComplete recognizes some types of editor controls (e.g. text boxes, combo boxes, and others) as standard Win32 controls, it extends them with extra methods (such as Click and Keys) that help you simulate actions over the controls. So, you can simulate actions over the cell editor controls in the same way as over usual text boxes, combo boxes and other controls.

The general sequence of actions should include: selecting the cell, activating its in-place editor, simulating actions that modify the cell value and saving the changes. In the example below, the SelectComboBoxItem routine illustrates this approach applied to the combo box editor:

3) Using internal properties and methods of a cell editor. You can use this approach if the tested application uses custom editor controls that TestComplete cannot recognize for some reason. In this case, you can access the current cell editor using the DataGridView.EditingControl property, and use methods and properties of the returned object to change the cell value.

The following example demonstrates how you can use the combo box editor’s SelectedItem and SelectedIndex properties to change the selected item:

Changing Values in Particular Cell Types

Changing a Text Cell Value

To change the text box cell value, you can “type” the desired text in it. You can simplify this process and send the keyboard input directly to the grid window instead of the cell editor. In the example below, the TypeToCell routine demonstrates how this can be done:

Changing Check Box Cell State

To change the state of a check box, you can simulate a click on it. Note that if you click in the center of the check box cell, that is, on the check mark, the cell’s check state is toggled. If you click the cell outside the check mark, the cell’s check state is not changed. For example, the ClickCell routine from the Selecting Cells in Microsoft DataGridView post clicks in the cell center, so it can be used to toggle the cell check state.

Selecting Items in Combo Box Cells Using Autocompletion

In DataGridView, the combo box editors support the automatic completion feature. However, this functionality is enabled if the AutoComplete property of the combo box cells column is True.

Autocompletion means that when you start typing in the combo box, the first item that matches the typed characters becomes selected. So, to select the desired item in the combo box cell, you can “type” the item caption into it. For instance, you can do this using the TypeToCell routine I used in the sample script.


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