Working With Specific In-place Editors in Microsoft DataGridView

Applies to TestComplete 15.70, last modified on December 17, 2024

The DataGridView control offers a number of column types, each providing a specific in-place editor for modifying cell values. This topic describes how you can change values in cells that use specific in-place editors such as check boxes and combo boxes.

The explanation below assumes that TestComplete has access to internal properties, methods and objects of the DataGridView control. For this purpose, the .NET Application Support and Microsoft Control Support plugins must be installed and enabled.
Accessing In-place Editors

When you start editing the grid cell value, that is, after you select the cell and activate the edit mode, the grid creates the corresponding edit control (in-place editor) for this. To access the current editor, use the grid’s internal EditingControl property. After the user has finished editing the cell value, the editor object is destroyed. After that, the EditingControl property returns the empty value. Thus, the editor object only exists and can be accessed when it is active. To check if the grid control is in the editing mode, you can use the grid’s IsCurrentCellInEditMode property.

After you obtain the object corresponding to the edit control, you can use its internal properties and methods to perform the desired actions. For example, you can use the Text property to get or set the editor’s current text, the DroppedDown property to display or hide the drop-down list, and so on.

Note that after you have modified the cell value, you should close the editor to save the changes made.

In the following sections, you will find information on performing typical operations over some in-place editors.

Working With Check Box Editors

To change the value of a check box cell, you can select the cell and simulate the Space key press (Space is a shortcut that checks or unchecks the check box). The following example demonstrates how you can change the state of check box cells in the DataGridView control. Note that the CheckCell routine works only for two-state check box cells.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // Change values in check box cells
  CheckCell (Grid, 0, "Discount", true);
  CheckCell (Grid, 1, "Discount", false);
}

function CheckCell (Grid, RowIndex, ColumnId, Checked)
{
  // Select the cell
  Grid.ClickCellXY (RowIndex, ColumnId, 1, 1);
  // Change the check box state
  if (Grid.wValue (RowIndex, ColumnId) != Checked)
    Grid.Keys (" ");
  // Apply the changes
  CloseCellEditor (Grid);
}

function CloseCellEditor (Grid)
{
  Grid.Keys ("[Enter]");
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridViewSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  # Change values in check box cells
  CheckCell (Grid, 0, "Discount", True)
  CheckCell (Grid, 1, "Discount", False)

def CheckCell (Grid, RowIndex, ColumnId, Checked):
  # Select the cell
  Grid.ClickCellXY (RowIndex, ColumnId, 1, 1)
  # Change the check box state
  if (Grid.wValue [RowIndex, ColumnId] != Checked):
    Grid.Keys (" ")
  # Apply the changes
  CloseCellEditor (Grid)

def CloseCellEditor (Grid):
  Grid.Keys ("[Enter]")

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridViewSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  ' Change values in check box cells
  Call CheckCell (Grid, 0, "Discount", True)
  Call CheckCell (Grid, 1, "Discount", False)
End Sub

Sub CheckCell (Grid, RowIndex, ColumnId, Checked)
  ' Select the cell
  Call Grid.ClickCellXY (RowIndex, ColumnId, 1, 1)
  ' Change the check box state
  If Grid.wValue (RowIndex, ColumnId) <> Checked Then
    Grid.Keys " "
  End If
  ' Apply the changes
  CloseCellEditor Grid
End Sub

Sub CloseCellEditor (Grid)
  Grid.Keys "[Enter]"
End Sub

DelphiScript

procedure CheckCell (Grid, RowIndex, ColumnId, Checked); forward;
procedure CloseCellEditor (Grid); forward;

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

  // Change values in check box cells
  CheckCell (Grid, 0, 'Discount', true);
  CheckCell (Grid, 1, 'Discount', false);
end;

procedure CheckCell (Grid, RowIndex, ColumnId, Checked);
begin
  // Select the cell
  Grid.ClickCellXY (RowIndex, ColumnId, 1, 1);
  // Change the check box state
  if not Grid.wValue (RowIndex, ColumnId).Equals (Checked) then
    Grid.Keys (' ');
  // Apply the changes
  CloseCellEditor (Grid);
end;

procedure CloseCellEditor (Grid);
begin
  Grid.Keys ('[Enter]');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("DataGridViewSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");

  // Change values in check box cells
  CheckCell (Grid, 0, "Discount", true);
  CheckCell (Grid, 1, "Discount", false);
}

function CheckCell (Grid, RowIndex, ColumnId, Checked)
{
  // Select the cell
  Grid["ClickCellXY"](RowIndex, ColumnId, 1, 1);
  // Change the check box state
  if (Grid["wValue"](RowIndex, ColumnId) != Checked)
    Grid["Keys"](" ");
  // Apply the changes
  CloseCellEditor (Grid);
}

function CloseCellEditor (Grid)
{
  Grid["Keys"]("[Enter]");
}

Working With Combo Box Editors

To change the selected item in the combo box editor, you can assign the caption of the desired item to the editor’s internal SelectedItem property. The SelectedIndex property lets you set the selected item by its zero-based index within the items list.

You can also work with the combo-box editor using methods and properties of a Win32ComboBox object. To be able to do that, you first need to use the Sys.WindowFromHandle function to obtain a window object corresponding to the combo box editor by its handle (the Handle property value). The returned object will contain “native” methods and properties of the combo box control as well as methods and properties of the Win32ComboBox object. For example, you will be able to select the desired combo box item using the Win32ComboBox.ClickItem action.

Example

View description

JavaScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // Change values in combo box cells
  ClickCellComboBoxItem (Grid, 0, "Product", "FamilyAlbum");
  ClickCellComboBoxItem (Grid, 0, "Card", "VISA");
}

function ClickCellComboBoxItem (Grid, RowIndex, ColumnId, Item)
{
  // Select the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Check if the cell uses the combo box editor
  if (equal(Grid.EditingControl.ClrClassName, "DataGridViewComboBoxEditingControl"))
  {
    // Get a Win32ComboBox object corresponding to the cell's editor
    let ComboBox = Sys.WindowFromHandle(Grid.EditingControl.Handle);
    // Select the specified item from the combo box
    ComboBox.ClickItem (Item);
    // Save the changes
    CloseCellEditor (Grid);
  }
  else
    Log.Error ("Cell (" + RowIndex + ", " + ColumnId + ") does not have a combo box editor.");
}

function CloseCellEditor (Grid)
{
  Grid.Keys ("[Enter]");
}

JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // Change values in combo box cells
  ClickCellComboBoxItem (Grid, 0, "Product", "FamilyAlbum");
  ClickCellComboBoxItem (Grid, 0, "Card", "VISA");
}

function ClickCellComboBoxItem (Grid, RowIndex, ColumnId, Item)
{
  // Select the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Check if the cell uses the combo box editor
  if (Grid.EditingControl.ClrClassName == "DataGridViewComboBoxEditingControl")
  {
    // Get a Win32ComboBox object corresponding to the cell's editor
    var ComboBox = Sys.WindowFromHandle(Grid.EditingControl.Handle);
    // Select the specified item from the combo box
    ComboBox.ClickItem (Item);
    // Save the changes
    CloseCellEditor (Grid);
  }
  else
    Log.Error ("Cell (" + RowIndex + ", " + ColumnId + ") does not have a combo box editor.");
}

function CloseCellEditor (Grid)
{
  Grid.Keys ("[Enter]");
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridViewSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  # Change values in combo box cells
  ClickCellComboBoxItem (Grid, 0, "Product", "FamilyAlbum")
  ClickCellComboBoxItem (Grid, 0, "Card", "VISA")

def ClickCellComboBoxItem (Grid, RowIndex, ColumnId, Item):
  # Select the cell
  Grid.ClickCell (RowIndex, ColumnId)
  # Activate the edit mode
  ActivateCellEditor (Grid)
  # Check if the cell uses the combo box editor
  if (Grid.EditingControl.ClrClassName == "DataGridViewComboBoxEditingControl"):
    # Get a Win32ComboBox object corresponding to the cell's editor
    ComboBox = Sys.WindowFromHandle(Grid.EditingControl.Handle)
    # Select the specified item from the combo box
    ComboBox.ClickItem (Item)
    # Save the changes
    CloseCellEditor (Grid)
  else:
    Log.Error ("Cell (" + RowIndex + ", " + ColumnId + ") does not have a combo box editor.")

def CloseCellEditor (Grid):
  Grid.Keys ("[Enter]")

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridViewSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  ' Change values in combo box cells
  Call ClickCellComboBoxItem (Grid, 0, "Product", "FamilyAlbum")
  Call ClickCellComboBoxItem (Grid, 0, "Card", "VISA")
End Sub

Sub ClickCellComboBoxItem (Grid, RowIndex, ColumnId, Item)
  Dim ComboBox

  ' Select the cell
  Call Grid.ClickCell (RowIndex, ColumnId)
  ' Activate the edit mode
  ActivateCellEditor Grid
  ' Check if the cell uses the combo box editor
  If Grid.EditingControl.ClrClassName = "DataGridViewComboBoxEditingControl" Then
    ' Get a Win32ComboBox object corresponding to the cell's editor
    Set ComboBox = Sys.WindowFromHandle(Grid.EditingControl.Handle)
    ' Select the specified item from the combo box
    ComboBox.ClickItem Item
    ' Save the changes
    CloseCellEditor Grid
  Else
    Log.Error "Cell (" & RowIndex & ", " & ColumnId & ") does not have a combo box editor."
  End If
End Sub

Sub CloseCellEditor (Grid)
  Grid.Keys "[Enter]"
End Sub

DelphiScript

procedure ClickCellComboBoxItem (Grid, RowIndex, ColumnId, Item); forward;
procedure CloseCellEditor (Grid); forward;

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

  // Change values in combo box cells
  ClickCellComboBoxItem (Grid, 0, 'Product', 'FamilyAlbum');
  ClickCellComboBoxItem (Grid, 0, 'Card', 'VISA');
end;

procedure ClickCellComboBoxItem (Grid, RowIndex, ColumnId, Item);
var ComboBox : OleVariant;
begin
  // Select the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Check if the cell uses the combo box editor
  if Grid.EditingControl.ClrClassName = 'DataGridViewComboBoxEditingControl' then
  begin
    // Get a Win32ComboBox object corresponding to the cell's editor
    ComboBox := Sys.WindowFromHandle(Grid.EditingControl.Handle);
    // Select the specified item from the combo box
    ComboBox.ClickItem (Item);
    // Save the changes
    CloseCellEditor (Grid);
  end
  else
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColumnId) + ') does not have a combo box editor.');
end;

procedure CloseCellEditor (Grid);
begin
  Grid.Keys ('[Enter]');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("DataGridViewSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");

  // Change values in combo box cells
  ClickCellComboBoxItem (Grid, 0, "Product", "FamilyAlbum");
  ClickCellComboBoxItem (Grid, 0, "Card", "VISA");
}

function ClickCellComboBoxItem (Grid, RowIndex, ColumnId, Item)
{
  // Select the cell
  Grid["ClickCell"](RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Check if the cell uses the combo box editor
  if (Grid["EditingControl"]["ClrClassName"] == "DataGridViewComboBoxEditingControl")
  {
    // Get a Win32ComboBox object corresponding to the cell's editor
    var ComboBox = Sys["WindowFromHandle"](Grid["EditingControl"]["Handle"]);
    // Select the specified item from the combo box
    ComboBox["ClickItem"](Item);
    // Save the changes
    CloseCellEditor (Grid);
  }
  else
    Log["Error"]("Cell (" + RowIndex + ", " + ColumnId + ") does not have a combo box editor.");
}

function CloseCellEditor (Grid)
{
  Grid["Keys"]("[Enter]");
}

The combo box editors may support the automatic completion feature. This means that when you start typing in the combo box, the first item that matches the typed characters becomes selected. Thus, to select the desired item in the combo box you can “type” the item caption into it. For instance, you can do this using the InputCellValue routine from the script example here: Obtaining and Setting Cell Values in Microsoft DataGridView.

See Also

Working With Microsoft DataGridView
Activating and Closing In-place Editors in Microsoft DataGridView
Obtaining and Setting Cell Values in Microsoft DataGridView
Copying and Pasting Cell Values in Microsoft DataGridView

Highlight search results