Working With Specific Cells in Syncfusion GridControl

Applies to TestComplete 15.20, last modified on January 19, 2022

Syncfusion's GridControl offers a variety of cell types: combo boxes, check boxes, radio buttons, spin boxes and cells with various drop-down editors. In the Obtaining and Setting Cell Values in Syncfusion GridControl topic, we defined the SetCellValue and InputCellValue routines that can be used to change grid cell values in general. However, you may need to modify the cell value in a specific way, for example, increase the value using the spin buttons, or simulate actions in the cell’s dropdown editor.

Generally, there are two approaches to modifying GridControl cell values:

  • Simulating user actions over cells, for example, clicking a cell’s check boxes and radio buttons.
  • Using internal properties and methods of the GridControl object.

The first approach is cumbersome, whereas, the second one is easier to implement. However, the first approach may be more preferable because it performs “real” actions over the grid and triggers the corresponding events of the grid control, like during normal application usage by the end users. When you modify cell values using the corresponding properties and methods of the grid control, events bound to user actions are not fired, so in this case the tested application may behave differently from what you expect.

The following sections explain how to perform typical operations over certain types of GridControl cells.

To perform these actions, TestComplete should have access to internal objects, properties and methods of the GridControl object. For this purpose, the .NET Application Support plugin must be installed and enabled.

When testing Syncfusion GridControl controls, use specific methods and properties of the corresponding SyncfusionEssGrid object. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with an object’s properties and methods from your scripts. However, when testing a GridControl control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

Clicking In-place Editors' Buttons

The GridControl can contain cells with embedded buttons, for example: cells with radio buttons or spin buttons, combo box cells and cells with drop-down editors. The cell buttons are usually used to modify the cell value in a special way, invoke custom dialogs, or perform other actions, so you may need to “press” these buttons in your test scripts.

To simulate a mouse click on the buttons, use the Click action. You need to know the desired button coordinates, which you can calculate using internal properties and methods of the GridControl object. The following example demonstrates how you can determine the cell’s button coordinates and simulate a button click.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // Click the cell's button
  ClickCellButton (Grid, 5, 5, 0);
  p.Window("#32770").Window("Button", "OK").ClickButton();

  // Click the 2nd button
  ClickCellButton (Grid, 7, 3, 2);
  p.Window("#32770").Window("Button", "OK").ClickButton();
}

function ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex)
{
  var CellRenderer, bounds;

  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the button coordinates
  CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex);
  CellRenderer.PerformLayout (RowIndex, ColIndex);
  bounds = CellRenderer.Buttons.Item(ButtonIndex).Bounds;

  Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2);
}

Python

def Main ():

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

  # Click the cell's button
  ClickCellButton (Grid, 5, 5, 0)
  p.Window("#32770").Window("Button", "OK").ClickButton()

  # Click the 2nd button
  ClickCellButton (Grid, 7, 3, 2)
  p.Window("#32770").Window("Button", "OK").ClickButton()

def ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex):

  # Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  # Get the button coordinates
  CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex)
  CellRenderer.PerformLayout (RowIndex, ColIndex)
  bounds = CellRenderer.Buttons.Item[ButtonIndex].Bounds

  Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2)

VBScript

Sub Main
  Dim p, Grid

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

  ' Click the cell's button
  Call ClickCellButton (Grid, 5, 5, 0)
  p.Window("#32770").Window("Button", "OK").ClickButton

  ' Click the 2nd button
  Call ClickCellButton (Grid, 7, 3, 2)
  p.Window("#32770").Window("Button", "OK").ClickButton
End Sub

Sub ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex)
  Dim CellRenderer, bounds

  ' Make cell visible
  Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  ' Get the button coordinates
  Set CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex)
  Call CellRenderer.PerformLayout (RowIndex, ColIndex)
  Set bounds = CellRenderer.Buttons.Item(ButtonIndex).Bounds

  Call Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2)
End Sub

DelphiScript

procedure ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex);
var CellRenderer, bounds : OleVariant;
begin
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the button coordinates
  CellRenderer := Grid.GetCellRenderer (RowIndex, ColIndex);
  CellRenderer.PerformLayout (RowIndex, ColIndex);
  bounds := CellRenderer.Buttons.Item[ButtonIndex].Bounds;

  Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2);
end;

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

  // Click the cell's button
  ClickCellButton (Grid, 5, 5, 0);
  p.Window('#32770').Window('Button', 'OK').ClickButton();

  // Click the cell's 2nd button
  ClickCellButton (Grid, 7, 3, 2);
  p.Window('#32770').Window('Button', 'OK').ClickButton();
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  // Click the cell's button
  ClickCellButton (Grid, 5, 5, 0);
  p["Window"]("#32770")["Window"]("Button", "OK")["ClickButton"]();

  // Click the 2nd button
  ClickCellButton (Grid, 7, 3, 2);
  p["Window"]("#32770")["Window"]("Button", "OK")["ClickButton"]();
}

function ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex)
{
  var CellRenderer, bounds;

  // Make cell visible
  Grid["ScrollCellInView_3"](RowIndex, ColIndex);

  // Get the button coordinates
  CellRenderer = Grid["GetCellRenderer"](RowIndex, ColIndex);
  CellRenderer["PerformLayout"](RowIndex, ColIndex);
  bounds = CellRenderer["Buttons"]["Item"](ButtonIndex)["Bounds"];

  Grid["Click"](bounds["X"] + bounds["Width"]/2, bounds["Y"] + bounds["Height"]/2);
}

Working With CheckBox Cells

You can change the checked state of a CheckBox cell in the following ways:

  • By simulating a mouse click on the check box using the Click action.
  • By selecting the cell and “pressing” the Space key using the Keys action. For more information on selecting cells in the GridControl, see Selecting Cells in Syncfusion GridControl.
  • By assigning the cell’s CellValue property the desired value: True (checked) or False (unchecked). If the check box works in the three-state mode (that is, if the cell’s TriState property is True), you can also put the cell into an indeterminate state by assigning the CellValue property with the corresponding value (usually, empty string). For more information on modifying cell values, see Obtaining and Setting Cell Values in Syncfusion GridControl.

The script sample below implements the first approach. It demonstrates how you can calculate the check box coordinates and simulate a mouse click on it.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process("CellControlsGrid");
  Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1");

  // Change the check box state
  ClickCellCheckBox (Grid, 14, 1, true);
}

function ClickCellCheckBox (Grid, RowIndex, ColIndex, Checked)
{
  var Cell, CellRenderer, bounds;

  // Get the cell object
  Cell = Grid.Item(RowIndex, ColIndex);

  // Check if the cell contains a check box
  if (Cell.CellType.OleValue == "CheckBox")
  {
    // Check if the current cell state is different from the desired one
    if (! Cell.CellValue.Equals (aqConvert.VarToStr(Checked)))
    {
      // Make cell visible
      Grid.ScrollCellInView_3 (RowIndex, ColIndex);

      // Get the check box coordinates
      CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex);
      CellRenderer.PerformLayout (RowIndex, ColIndex);
      bounds = CellRenderer.GetCachedCheckerBounds (RowIndex, ColIndex);

      Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2);
    }
  }
  else 
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not contain a check box.");
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("CellControlsGrid")
  Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1")

  # Change the check box state
  ClickCellCheckBox (Grid, 14, 1, True)

def ClickCellCheckBox (Grid, RowIndex, ColIndex, Checked):

  # Get the cell object
  Cell = Grid.Item(RowIndex, ColIndex)

  # Check if the cell contains a check box
  if (Cell.CellType.OleValue == "CheckBox"):
    # Check if the current cell state is different from the desired one
    if not Cell.CellValue.Equals (aqConvert.VarToStr(Checked)):
      # Make cell visible
      Grid.ScrollCellInView_3 (RowIndex, ColIndex)

      # Get the check box coordinates
      CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex)
      CellRenderer.PerformLayout (RowIndex, ColIndex)
      bounds = CellRenderer.GetCachedCheckerBounds (RowIndex, ColIndex)

      Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2)
  else: 
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not contain a check box.")

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process("CellControlsGrid")
  Set Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1")

  ' Change the check box state
  Call ClickCellCheckBox (Grid, 14, 1, True)
End Sub

Sub ClickCellCheckBox (Grid, RowIndex, ColIndex, Checked)
  Dim Cell, CellRenderer, bounds

  ' Get the cell object
  Set Cell = Grid.Item(RowIndex, ColIndex)

  ' Check if the cell contains a check box
  If Cell.CellType.OleValue = "CheckBox" Then
    ' Check if the current cell state is different from the desired one
    If Not Cell.CellValue.Equals (aqConvert.VarToStr(Checked)) Then
      ' Make cell visible
      Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)

      ' Get the check box coordinates
      Set CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex)
      Call CellRenderer.PerformLayout (RowIndex, ColIndex)
      Set bounds = CellRenderer.GetCachedCheckerBounds (RowIndex, ColIndex)

      Call Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2)
    End If
  Else 
    Call Log.Error ("Cell (" & RowIndex & ", " & ColIndex & ") does not contain a check box.")
  End If
End Sub

DelphiScript

procedure ClickCellCheckBox (Grid, RowIndex, ColIndex, Checked);
var Cell, CellRenderer, bounds : OleVariant;
begin
  // Get the cell object
  Cell := Grid.Item[RowIndex, ColIndex];

  // Check if the cell contains a check box
  if Cell.CellType.OleValue = 'CheckBox' then
  begin
    // Check if the current cell state is different from the desired one
    if not Cell.CellValue.Equals (aqConvert.VarToStr(Checked)) then
    begin
      // Make cell visible
      Grid.ScrollCellInView_3 (RowIndex, ColIndex);

      // Get the check box coordinates
      CellRenderer := Grid.GetCellRenderer(RowIndex, ColIndex);
      CellRenderer.PerformLayout (RowIndex, ColIndex);
      bounds := CellRenderer.GetCachedCheckerBounds(RowIndex, ColIndex);

      Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2);
    end
  end
  else 
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColIndex) + ') does not contain a check box.');
end;

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('CellControlsGrid');
  Grid := p.WinFormsObject('CellControlsForm').WinFormsObject('gridControl1');

  // Change the check box state
  ClickCellCheckBox (Grid, 14, 1, true);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("CellControlsGrid");
  Grid = p["WinFormsObject"]("CellControlsForm")["WinFormsObject"]("gridControl1");

  // Change the check box state
  ClickCellCheckBox (Grid, 14, 1, true);
}

function ClickCellCheckBox (Grid, RowIndex, ColIndex, Checked)
{
  var Cell, CellRenderer, bounds;

  // Get the cell object
  Cell = Grid["Item"](RowIndex, ColIndex);

  // Check if the cell contains a check box
  if (Cell["CellType"]["OleValue"] == "CheckBox")
  {
    // Check if the current cell state is different from the desired one
    if (! Cell["CellValue"]["Equals"](aqConvert["VarToStr"](Checked)))
    {
      // Make cell visible
      Grid["ScrollCellInView_3"](RowIndex, ColIndex);

      // Get the check box coordinates
      CellRenderer = Grid["GetCellRenderer"](RowIndex, ColIndex);
      CellRenderer["PerformLayout"](RowIndex, ColIndex);
      bounds = CellRenderer["GetCachedCheckerBounds"](RowIndex, ColIndex);

      Grid["Click"](bounds["X"] + bounds["Width"]/2, bounds["Y"] + bounds["Height"]/2);
    }
  }
  else 
    Log["Error"]("Cell (" + RowIndex + ", " + ColIndex + ") does not contain a check box.");
}

Working With RadioButton Cells

To change the selected radio button in a RadioButton cell, you can do any of the following:

  • Simulate a mouse click on the desired radio button. For example, you can do this using the ClickCellButton routine defined above in the Clicking In-place Editors' Buttons section.
  • Select the cell and then use Right Arrow or Left Arrow keys to select the desired radio button. To simulate arrow key presses, use the Keys action.
  • Assign the zero-based index of the desired button to the cell’s CellValue property.

We use the first approach, in the example below, to simulate a click over the needed radio button.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process("RadioButtonCells");
  Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("splitterControl1").WinFormsObject("gridControl1");

  // Select a radio button by caption
  ClickCellRadioButton (Grid, 2, 2, "button 2");

  // Select a radio button by index
  ClickCellRadioButton (Grid, 4, 2, 1);
}

function ClickCellRadioButton (Grid, RowIndex, ColIndex, Item)
{
  var Cell, ItemIndex;

  // Check if the cell contains radio buttons
  Cell = Grid.Item (RowIndex, ColIndex);
  if (Cell.CellType.OleValue == "RadioButton")
  {
    // Determine the radio button index
    if (aqObject.GetVarType (Item) == varOleStr)
      ItemIndex = Cell.ChoiceList.IndexOf (Item);
    else
      ItemIndex = Item;

    ClickCellButton (Grid, RowIndex, ColIndex, ItemIndex);
  }
  else
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not contain radio buttons.");
}

function ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex)
{
  var CellRenderer, bounds;

  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get button coordinates
  CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex);
  CellRenderer.PerformLayout (RowIndex, ColIndex);
  bounds = CellRenderer.Buttons.Item(ButtonIndex).Bounds;

  Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("RadioButtonCells")
  Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("splitterControl1").WinFormsObject("gridControl1")

  # Select a radio button by caption
  ClickCellRadioButton (Grid, 2, 2, "button 2")

  # Select a radio button by index
  ClickCellRadioButton (Grid, 4, 2, 1)

def ClickCellRadioButton (Grid, RowIndex, ColIndex, Item):

  # Check if the cell contains radio buttons
  Cell = Grid.Item (RowIndex, ColIndex)
  if (Cell.CellType.OleValue == "RadioButton"):
    # Determine the radio button index
    if (aqObject.GetVarType (Item) == varOleStr):
      ItemIndex = Cell.ChoiceList.IndexOf (Item)
    else:
      ItemIndex = Item

    ClickCellButton (Grid, RowIndex, ColIndex, ItemIndex)
  else:
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not contain radio buttons.")

def ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex):

  # Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  # Get button coordinates
  CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex)
  CellRenderer.PerformLayout (RowIndex, ColIndex)
  bounds = CellRenderer.Buttons.Item[ButtonIndex].Bounds

  Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2)

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process("RadioButtonCells")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("splitterControl1").WinFormsObject("gridControl1")

  ' Select a radio button by caption
  Call ClickCellRadioButton (Grid, 2, 2, "button 2")

  ' Select a radio button by index
  Call ClickCellRadioButton (Grid, 4, 2, 1)
End Sub

Sub ClickCellRadioButton (Grid, RowIndex, ColIndex, Item)
  Dim Cell, ItemIndex

 ' Check if the cell contains radio buttons
  Set Cell = Grid.Item (RowIndex, ColIndex)
  If Cell.CellType.OleValue = "RadioButton" Then
    ' Determine the radio button index
    If aqObject.GetVarType (Item) = varOleStr Then
      ItemIndex = Cell.ChoiceList.IndexOf (Item)
    Else
      ItemIndex = Item
    End If

    Call ClickCellButton (Grid, RowIndex, ColIndex, ItemIndex)
  Else
    Call Log.Error ("Cell (" & RowIndex & ", " & ColIndex & ") does not contain radio buttons.")
  End If
End Sub

Sub ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex)
  Dim CellRenderer, bounds

  ' Make cell visible
  Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  ' Get button coordinates
  Set CellRenderer = Grid.GetCellRenderer (RowIndex, ColIndex)
  Call CellRenderer.PerformLayout (RowIndex, ColIndex)
  Set bounds = CellRenderer.Buttons.Item(ButtonIndex).Bounds

  Call Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2)
End Sub

DelphiScript

procedure ClickCellRadioButton (Grid, RowIndex, ColIndex, Item); forward;
procedure ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex); forward;

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

  // Select a radio button by caption
  ClickCellRadioButton (Grid, 2, 2, 'button 2');

  // Select a radio button by index
  ClickCellRadioButton (Grid, 4, 2, 1);
end;

procedure ClickCellRadioButton (Grid, RowIndex, ColIndex, Item);
var Cell, ItemIndex : OleVariant;
begin
  // Check if the cell contains radio buttons
  Cell := Grid.Item [RowIndex, ColIndex];
  if Cell.CellType.OleValue = 'RadioButton' then
  begin
    // Determine the radio button index
    if aqObject.GetVarType (Item) = varOleStr then
      ItemIndex := Cell.ChoiceList.IndexOf (Item)
    else
      ItemIndex := Item;

    ClickCellButton (Grid, RowIndex, ColIndex, ItemIndex);
  end
  else
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColIndex) + ') does not contain radio buttons.');
end;

procedure ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex);
var CellRenderer, bounds : OleVariant;
begin
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get button coordinates
  CellRenderer := Grid.GetCellRenderer (RowIndex, ColIndex);
  CellRenderer.PerformLayout (RowIndex, ColIndex);
  bounds := CellRenderer.Buttons.Item[ButtonIndex].Bounds;

  Grid.Click (bounds.X + bounds.Width/2, bounds.Y + bounds.Height/2);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("RadioButtonCells");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("panel1")["WinFormsObject"]("splitterControl1")["WinFormsObject"]("gridControl1");

  // Select a radio button by caption
  ClickCellRadioButton (Grid, 2, 2, "button 2");

  // Select a radio button by index
  ClickCellRadioButton (Grid, 4, 2, 1);
}

function ClickCellRadioButton (Grid, RowIndex, ColIndex, Item)
{
  var Cell, ItemIndex;

  // Check if the cell contains radio buttons
  Cell = Grid["Item"](RowIndex, ColIndex);
  if (Cell["CellType"]["OleValue"] == "RadioButton")
  {
    // Determine the radio button index
    if (aqObject["GetVarType"] (Item) == varOleStr)
      ItemIndex = Cell["ChoiceList"]["IndexOf"](Item);
    else
      ItemIndex = Item;

    ClickCellButton (Grid, RowIndex, ColIndex, ItemIndex);
  }
  else
    Log["Error"]("Cell (" + RowIndex + ", " + ColIndex + ") does not contain radio buttons.");
}

function ClickCellButton (Grid, RowIndex, ColIndex, ButtonIndex)
{
  var CellRenderer, bounds;

  // Make cell visible
  Grid["ScrollCellInView_3"](RowIndex, ColIndex);

  // Get button coordinates
  CellRenderer = Grid["GetCellRenderer"](RowIndex, ColIndex);
  CellRenderer["PerformLayout"](RowIndex, ColIndex);
  bounds = CellRenderer["Buttons"]["Item"](ButtonIndex)["Bounds"];

  Grid["Click"](bounds["X"] + bounds["Width"]/2, bounds["Y"] + bounds["Height"]/2);
}

Working With PushButton Cells

You can “press” a PushButton cell by simulating a mouse click on it. In the Selecting Cells in Syncfusion GridControl topic, we have explained how you can simulate cell clicks. The following example uses the ClickCell routine from that topic to perform a click on a PushButton cell.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the application process and the grid object
  p = Sys.Process("CellControlsGrid");
  Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1");

  // Click a push button
  ClickCell (Grid, 17, 3);

  // Simulate actions in the popup window
  p.Window("#32770").Window("Button", "OK").ClickButton();
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get cell coordinates
  var rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
}

Python

def Main ():

  # Obtain the application process and the grid object
  p = Sys.Process("CellControlsGrid")
  Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1")

  # Click a push button
  ClickCell (Grid, 17, 3)

  # Simulate actions in the popup window
  p.Window("#32770").Window("Button", "OK").ClickButton() 

def ClickCell (Grid, RowIndex, ColIndex):
  # Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  # Get cell coordinates
  rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell[RowIndex, ColIndex])

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the application process and the grid object
  Set p = Sys.Process("CellControlsGrid")
  Set Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1")

  ' Click a push button
  Call ClickCell (Grid, 17, 3)

  ' Simulate actions in the popup window
  p.Window("#32770").Window("Button", "OK").ClickButton
End Sub

Sub ClickCell (Grid, RowIndex, ColIndex)
  Dim rect
  ' Make cell visible
  Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)
  ' Get cell coordinates
  Set rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex))
  Call Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)
End Sub

DelphiScript

procedure ClickCell (Grid, RowIndex, ColIndex);
var rect : OleVariant;
begin
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get cell coordinates
  rect := Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
end;

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the application process and the grid object
  p := Sys.Process('CellControlsGrid');
  Grid := p.WinFormsObject('CellControlsForm').WinFormsObject('gridControl1');

  // Click a push button
  ClickCell (Grid, 17, 3);

  // Simulate actions in the popup window
  p.Window('#32770').Window('Button', 'OK').ClickButton();
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the application process and the grid object
  p = Sys["Process"]("CellControlsGrid");
  Grid = p["WinFormsObject"]("CellControlsForm")["WinFormsObject"]("gridControl1");

  // Click a push button
  ClickCell (Grid, 17, 3);

  // Simulate actions in the popup window
  p["Window"]("#32770")["Window"]("Button", "OK")["ClickButton"]();
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid["ScrollCellInView_3"](RowIndex, ColIndex);

  // Get cell coordinates
  var rect = Grid["RangeInfoToRectangle"](Grid["GridCellsRange"]["Cell"](RowIndex, ColIndex));

  Grid["Click"](rect["X"] + rect["Width"]/2, rect["Y"] + rect["Height"]/2);
}

Working With Drop-Down Editors

Syncfusion GridControl offers various cells that use drop-down editors. For example, a ComboBox cell uses a drop-down list of items, a MonthCalendar cell uses a drop-down window containing a Month Calendar control.

You can display a drop-down editor in the following ways:

  • By simulating a mouse click on the cell’s down arrow button.
  • By selecting a cell and pressing the F4 key.
  • By selecting a cell and calling the CurrentCell.ShowDropDown method of the GridControl object.

You can also use the following properties and methods of the GridControl object to check whether the popup window is currently open and to close it:

  • CurrentCell.IsDropedDown: Returns True if the drop-down editor is currently opened, and False otherwise.
  • CurrentCell.CloseDropDown(Reason): Closes the drop-down editor. The Reason parameter specifies whether to save the changes made; pass the “Done” value in order to save the changes and “Cancel” to discard changes.

To get access to the drop-down window, use the dedicated property of a grid’s CurrentCellRenderer object. For example, the CurrentCellRenderer.ListBoxPart can be used to obtain the drop-down combo box or GridListControl, the CurrentCellRenderer.calendar property returns the drop-down Month Calendar control, and so on.

Note that in this way, you obtain the “native” .NET object corresponding to the editor. To obtain a window or a Win32 object corresponding to the editor control, you can use the Sys.WindowFromHandle method and pass the control’s handle (the Handle property value) as a parameter. You can then use properties, methods and actions of the obtained object to simulate user actions over the editor.

The following example illustrates how you can open drop-down editors (namely, a combo box and Month Calendar editors), access them and simulate user actions over them.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid, Calendar;

  // Obtain the grid object
  p = Sys.Process("CellControlsGrid");
  Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1");

  // Select items in ComboBox cells
  ClickCellComboBoxItem (Grid, 23, 1, "One");
  ClickCellComboBoxItem (Grid, 23, 3, 3);

  // Simulate actions in drop-down MonthCalendar controls
  Calendar = OpenMonthCalendar (Grid, 29, 1);
  Calendar.SetSelection ("5/1/2007");

  Calendar = OpenMonthCalendar (Grid, 29, 3);
  Calendar.SetSelection ("7/4/2007");
}

function ClickCellComboBoxItem (Grid, RowIndex, ColIndex, Item)
{
  // Check if the specified cell is of the ComboBox type
  if (Grid.Item (RowIndex, ColIndex).CellType.OleValue == "ComboBox")
  {
    // Select the cell and display the drop-down list
    ClickCell (Grid, RowIndex, ColIndex);
    if (! Grid.CurrentCell.IsDroppedDown)
      Grid.Keys ("[F4]");

    // Get the onscreen object corresponding to the combo box
    var ComboBox = Sys.WindowFromHandle(Grid.CurrentCellRenderer.ListBoxPart.Handle);
    // Click the specified combo box item
    ComboBox.ClickItem (Item);
  }
  else
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not have a combo box.");
}

function OpenMonthCalendar (Grid, RowIndex, ColIndex)
{
  // Check if the cell is of the MonthCalendar type
  if (Grid.Item (RowIndex, ColIndex).CellType.OleValue == "MonthCalendar")
  {
    // Select the cell and invoke the drop-down editor
    ClickCell (Grid, RowIndex, ColIndex);
    if (! Grid.CurrentCell.IsDroppedDown)
      Grid.Keys ("[F4]");

    // Get an onscreen object corresponding to the MonthCalendar editor
    return Sys.WindowFromHandle(Grid.CurrentCellRenderer.calendar.Handle);
  }
  else
  {
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not have a drop-down MonthCalendar.");
    return null;
  }
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the cell coordinates
  var rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("CellControlsGrid")
  Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1")

  # Select items in ComboBox cells
  ClickCellComboBoxItem (Grid, 23, 1, "One")
  ClickCellComboBoxItem (Grid, 23, 3, 3)

  # Simulate actions in drop-down MonthCalendar controls
  Calendar = OpenMonthCalendar (Grid, 29, 1)
  Calendar.SetSelection ("5/1/2007")

  Calendar = OpenMonthCalendar (Grid, 29, 3)
  Calendar.SetSelection ("7/4/2007")

def ClickCellComboBoxItem (Grid, RowIndex, ColIndex, Item):
  # Check if the specified cell is of the ComboBox type
  if (Grid.Item (RowIndex, ColIndex).CellType.OleValue == "ComboBox"):
    # Select the cell and display the drop-down list
    ClickCell (Grid, RowIndex, ColIndex)
    if not Grid.CurrentCell.IsDroppedDown:
      Grid.Keys ("[F4]")

    # Get the onscreen object corresponding to the combo box
    ComboBox = Sys.WindowFromHandle(Grid.CurrentCellRenderer.ListBoxPart.Handle)
    # Click the specified combo box item
    ComboBox.ClickItem (Item)
  else:
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not have a combo box.")

def OpenMonthCalendar (Grid, RowIndex, ColIndex):
  # Check if the cell is of the MonthCalendar type
  if (Grid.Item (RowIndex, ColIndex).CellType.OleValue == "MonthCalendar"):
    # Select the cell and invoke the drop-down editor
    ClickCell (Grid, RowIndex, ColIndex)
    if not Grid.CurrentCell.IsDroppedDown:
      Grid.Keys ("[F4]")

    # Get an onscreen object corresponding to the MonthCalendar editor
    return Sys.WindowFromHandle(Grid.CurrentCellRenderer.calendar.Handle)
  else:
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") does not have a drop-down MonthCalendar.")
    return None

def ClickCell (Grid, RowIndex, ColIndex):
  # Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  # Get the cell coordinates
  rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex))

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)

VBScript

Sub Main
  Dim p, Grid, Calendar

  ' Obtain the grid object
  Set p = Sys.Process("CellControlsGrid")
  Set Grid = p.WinFormsObject("CellControlsForm").WinFormsObject("gridControl1")

  ' Select items in ComboBox cells
  Call ClickCellComboBoxItem_2 (Grid, 23, 1, "One")
  Call ClickCellComboBoxItem_2 (Grid, 23, 3, 3)

  ' Simulate actions in drop-down MonthCalendar controls
  Set Calendar = OpenMonthCalendar (Grid, 29, 1)
  Call Calendar.SetSelection ("5/1/2007")
  Set Calendar = OpenMonthCalendar (Grid, 29, 3)
  Call Calendar.SetSelection ("7/4/2007")
End Sub

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

  ' Check if the specified cell is of the ComboBox type
  If Grid.Item(RowIndex, ColIndex).CellType.OleValue = "ComboBox" Then
    ' Select the cell and display the drop-down list
    Call ClickCell (Grid, RowIndex, ColIndex)
    If Not Grid.CurrentCell.IsDroppedDown Then
      Grid.Keys ("[F4]")
    End If

    ' Get the onscreen object corresponding to the combo box
    Set ComboBox = Sys.WindowFromHandle(Grid.CurrentCellRenderer.ListBoxPart.Handle)
    ' Click the specified combo box item
    Call ComboBox.ClickItem (Item)
  Else
    Log.Error ("Cell (" & RowIndex & ", " & ColIndex & ") does not have a combo box.")
  End If
End Sub

Function OpenMonthCalendar (Grid, RowIndex, ColIndex)
  ' Check if the cell is of the MonthCalendar type
  If Grid.Item(RowIndex, ColIndex).CellType.OleValue = "MonthCalendar" Then
  
    ' Select the cell and invoke the drop-down editor
    Call ClickCell (Grid, RowIndex, ColIndex)
    If Not Grid.CurrentCell.IsDroppedDown Then
      Grid.Keys ("[F4]")
    End If

    ' Get an onscreen object corresponding to the MonthCalendar editor
    Set OpenMonthCalendar = Sys.WindowFromHandle(Grid.CurrentCellRenderer.calendar.Handle)
  Else
    Call Log.Error ("Cell (" & RowIndex & ", " & ColIndex & ") does not have a drop-down MonthCalendar.")
    Set OpenMonthCalendar = Nothing
  End If
End Function

Sub ClickCell (Grid, RowIndex, ColIndex)
  Dim rect

  ' Make cell visible
  Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  ' Get the cell coordinates
  Set rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex))

  Call Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)
End Sub

DelphiScript

procedure ClickCellComboBoxItem (Grid, RowIndex, ColIndex, Item); forward;
function OpenMonthCalendar (Grid, RowIndex, ColIndex); forward;
procedure ClickCell (Grid, RowIndex, ColIndex); forward;

procedure Main;
var p, Grid, Calendar : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('CellControlsGrid');
  Grid := p.WinFormsObject('CellControlsForm').WinFormsObject('gridControl1');

  // Select items in ComboBox cells
  ClickCellComboBoxItem (Grid, 23, 1, 'One');
  ClickCellComboBoxItem (Grid, 23, 3, 3);

  // Simulate actions in drop-down MonthCalendar controls
  Calendar := OpenMonthCalendar (Grid, 29, 1);
  Calendar.SetSelection ('5/1/2007');
  Calendar := OpenMonthCalendar (Grid, 29, 3);
  Calendar.SetSelection ('7/4/2007');
end;

procedure ClickCellComboBoxItem (Grid, RowIndex, ColIndex, Item);
var ComboBox : OleVariant;
begin
  // Check if the specified cell is of the ComboBox type
  if Grid.Item[RowIndex, ColIndex].CellType.OleValue = 'ComboBox' then
  begin
    // Select the cell and display the drop-down list
    ClickCell (Grid, RowIndex, ColIndex);
    if not Grid.CurrentCell.IsDroppedDown then
      Grid.Keys ('[F4]');

    // Get the onscreen object corresponding to the combo box
    ComboBox := Sys.WindowFromHandle(Grid.CurrentCellRenderer.ListBoxPart.Handle);
    // Click the specified combo box item
    ComboBox.ClickItem (Item);
  end
  else
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColIndex) + ') does not have a combo box.');
end;

function OpenMonthCalendar (Grid, RowIndex, ColIndex);
begin
  // Check if the cell is of the MonthCalendar type
  if Grid.Item[RowIndex, ColIndex].CellType.OleValue = 'MonthCalendar' then
  begin
    // Select the cell and invoke the drop-down editor
    ClickCell (Grid, RowIndex, ColIndex);
    if not Grid.CurrentCell.IsDroppedDown then
      Grid.Keys ('[F4]');

    // Get an onscreen object corresponding to the MonthCalendar editor
    Result := Sys.WindowFromHandle(Grid.CurrentCellRenderer.calendar.Handle);
  end
  else
  begin
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColIndex) + ') does not have a drop-down MonthCalendar.');
    Result := nil;
  end
end;

procedure ClickCell (Grid, RowIndex, ColIndex);
var rect : OleVariant;
begin
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the cell coordinates
  rect := Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, Calendar;

  // Obtain the grid object
  p = Sys["Process"]("CellControlsGrid");
  Grid = p["WinFormsObject"]("CellControlsForm")["WinFormsObject"]("gridControl1");

  // Select items in ComboBox cells
  ClickCellComboBoxItem (Grid, 23, 1, "One");
  ClickCellComboBoxItem (Grid, 23, 3, 3);

  // Simulate actions in drop-down MonthCalendar controls
  Calendar = OpenMonthCalendar (Grid, 29, 1);
  Calendar["SetSelection"]("5/1/2007");

  Calendar = OpenMonthCalendar (Grid, 29, 3);
  Calendar["SetSelection"]("7/4/2007");
}

function ClickCellComboBoxItem (Grid, RowIndex, ColIndex, Item)
{
  // Check if the specified cell is of the ComboBox type
  if (Grid["Item"](RowIndex, ColIndex)["CellType"]["OleValue"] == "ComboBox")
  {
    // Select the cell and display the drop-down list
    ClickCell (Grid, RowIndex, ColIndex);
    if (! Grid["CurrentCell"]["IsDroppedDown"])
      Grid["Keys"]("[F4]");

    // Get the onscreen object corresponding to the combo box
    var ComboBox = Sys["WindowFromHandle"](Grid["CurrentCellRenderer"]["ListBoxPart"]["Handle"]);
    // Click the specified combo box item
    ComboBox["ClickItem"](Item);
  }
  else
    Log["Error"]("Cell (" + RowIndex + ", " + ColIndex + ") does not have a combo box.");
}

function OpenMonthCalendar (Grid, RowIndex, ColIndex)
{
  // Check if the cell is of the MonthCalendar type
  if (Grid["Item"](RowIndex, ColIndex)["CellType"]["OleValue"] == "MonthCalendar")
  {
    // Select the cell and invoke the drop-down editor
    ClickCell (Grid, RowIndex, ColIndex);
    if (! Grid["CurrentCell"]["IsDroppedDown"])
      Grid["Keys"]("[F4]");

    // Get an onscreen object corresponding to the MonthCalendar editor
    return Sys["WindowFromHandle"](Grid["CurrentCellRenderer"]["calendar"]["Handle"]);
  }
  else
  {
    Log["Error"]("Cell (" + RowIndex + ", " + ColIndex + ") does not have a drop-down MonthCalendar.");
    return null;
  }
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid["ScrollCellInView_3"](RowIndex, ColIndex);

  // Get the cell coordinates
  var rect = Grid["RangeInfoToRectangle"](Grid["GridCellsRange"]["Cell"](RowIndex, ColIndex));

  Grid["Click"](rect["X"] + rect["Width"]/2, rect["Y"] + rect["Height"]/2);
}

Note that most drop-down editors in GridControl support the automatic completion feature. It means that you can select an item or value from the editor by typing the item caption into the cell. For more information about simulating input strings into GridControl cells, see Obtaining and Setting Cell Values in Syncfusion GridControl.

See Also

Working With Syncfusion GridControl
Activating and Closing In-place Editors in Syncfusion GridControl
Obtaining and Setting Cell Values in Syncfusion GridControl
Selecting Cells in Syncfusion GridControl

Highlight search results