In order to perform various actions over Syncfusion GridControl, you first need to locate the row that contains the data you are going to work with. This topic describes how you can search for the desired grid record.
The GridControl object does not have built-in methods for searching within the grid, so you will need to implement the search algorithm yourself. A general search approach implies that you iterate through the grid rows and on each iteration check whether the row meets the specified condition. For example, you can check if the cell text or value in a particular column is equal to the specified text or value. (To learn how you can get GridControl cell values and their display text, see Obtaining and Setting Cell Values in Syncfusion GridControl.)
The script below demonstrates how you can locate a particular row by iterating through the grid rows. It contains two sample functions named FindRow
and FindRowByCellText
, that search for the grid row by a cell’s value and text, respectively.
Example
JavaScript, JScript
function Main ()
{
var p, Grid, SearchText, SearchValue, RowIndex;
// Obtain the application process and the grid object
p = Sys.Process("TextFormat");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1");
// Locate the row by cell text
SearchText = aqConvert.DateTimeToFormatStr (aqDateTime.Today(), "%A, %B %d, %Y");
RowIndex = FindRowByCellText (Grid, 2, SearchText);
if (RowIndex != -1)
ClickCell (Grid, RowIndex, 2)
else
Log.Error ("Row with text '" + SearchText + "' was not found.");
// Locate the row by cell value
SearchValue = Math.PI; // 3.14159265358979
RowIndex = FindRow (Grid, 2, SearchValue);
if (RowIndex != -1)
ClickCell (Grid, RowIndex, 2)
else
Log.Error ("Row with value '" + aqConvert.VarToStr(SearchValue) + "' was not found.");
}
function FindRow (Grid, ColIndex, Value)
{
var StartRowIndex, EndRowIndex, i;
// Get indexes of the first and last data rows
StartRowIndex = Grid.GridCellsRange.Top;
EndRowIndex = Grid.GridCellsRange.Bottom;
// Iterate through data rows
for (i=StartRowIndex; i<=EndRowIndex; i++)
// Compare the cell value with the specified value
if (Grid.Item(i, ColIndex).CellValue.Equals(Value))
return i; // Row is found;
return -1; // Row is not found
}
function FindRowByCellText (Grid, ColIndex, Text)
{
var StartRowIndex, EndRowIndex, i;
// Get indexes of the first and last data rows
StartRowIndex = Grid.GridCellsRange.Top;
EndRowIndex = Grid.GridCellsRange.Bottom;
// Iterate through data rows
for (i=StartRowIndex; i<=EndRowIndex; i++)
// Compare the cell's display text with the specified string
if (Grid.Item(i, ColIndex).FormattedText.OleValue == Text)
return i; // Row is found;
return -1; // Row is not found
}
function 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);
}
Python
def Main ():
# Obtain the application process and the grid object
p = Sys.Process("TextFormat")
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
# Locate the row by cell text
SearchText = aqConvert.DateTimeToFormatStr (aqDateTime.Today(), "%A, %B %d, %Y")
RowIndex = FindRowByCellText (Grid, 2, SearchText)
if (RowIndex != -1):
ClickCell (Grid, RowIndex, 2)
else:
Log.Error ("Row with text '" + SearchText + "' was not found.")
# Locate the row by cell value
SearchValue = Math.PI # 3.14159265358979
RowIndex = FindRow (Grid, 2, SearchValue)
if (RowIndex != -1):
ClickCell (Grid, RowIndex, 2)
else:
Log.Error ("Row with value '" + aqConvert.VarToStr(SearchValue) + "' was not found.")
def FindRow (Grid, ColIndex, Value):
# Get indexes of the first and last data rows
StartRowIndex = Grid.GridCellsRange.Top
EndRowIndex = Grid.GridCellsRange.Bottom
# Iterate through data rows
for i in range(StartRowIndex, EndRowIndex-1):
# Compare the cell value with the specified value
if (Grid.Item[i, ColIndex].CellValue.Equals(Value)):
return i # Row is found
return -1 # Row is not found
def FindRowByCellText (Grid, ColIndex, Text):
# Get indexes of the first and last data rows
StartRowIndex = Grid.GridCellsRange.Top
EndRowIndex = Grid.GridCellsRange.Bottom
# Iterate through data rows
for i in range(StartRowIndex, EndRowIndex-1):
# Compare the cell's display text with the specified string
if (Grid.Item[i, ColIndex].FormattedText.OleValue == Text):
return i # Row is found
return -1 # Row is not found
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, SearchText, SearchValue, RowIndex
' Obtain the application process and the grid object
Set p = Sys.Process("TextFormat")
Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
' Locate the row by cell text
SearchText = aqConvert.DateTimeToFormatStr (aqDateTime.Today(), "%A, %B %d, %Y")
RowIndex = FindRowByCellText (Grid, 2, SearchText)
If RowIndex <> -1 Then
Call ClickCell (Grid, RowIndex, 2)
Else
Call Log.Error ("Row with text '" & SearchText & "' was not found.")
End If
' Locate the row by cell value
SearchValue = 4*Atn (1) ' Gets the value of Pi (3.14159265358979)
RowIndex = FindRow (Grid, 2, SearchValue)
If RowIndex <> -1 Then
Call ClickCell (Grid, RowIndex, 2)
Else
Call Log.Error ("Row with value '" & aqConvert.VarToStr(SearchValue) & "' was not found.")
End If
End Sub
Function FindRow (Grid, ColIndex, Value)
Dim StartRowIndex, EndRowIndex, i
' Get indexes of the first and last data rows
StartRowIndex = Grid.GridCellsRange.Top
EndRowIndex = Grid.GridCellsRange.Bottom
' Iterate through data rows
For i = StartRowIndex To EndRowIndex
' Compare the cell value with the specified value
If Grid.Item(i, ColIndex).CellValue.Equals(Value) Then
FindRow = i ' Row is found
Exit Function
End If
Next
FindRow = -1 ' Row is not found
End Function
function FindRowByCellText (Grid, ColIndex, Text)
Dim StartRowIndex, EndRowIndex, i
' Get indexes of the first and last data rows
StartRowIndex = Grid.GridCellsRange.Top
EndRowIndex = Grid.GridCellsRange.Bottom
' Iterate through data rows
For i = StartRowIndex To EndRowIndex
' Compare the cell's display text with the specified string
If Grid.Item(i, ColIndex).FormattedText.OleValue = Text Then
FindRowByCellText = i ' Row is found
Exit Function
End If
Next
FindRowByCellText = -1 ' Row is not found
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
function FindRow (Grid, ColIndex, Value); forward;
function FindRowByCellText (Grid, ColIndex, Text); forward;
procedure ClickCell (Grid, RowIndex, ColIndex); forward;
procedure Main;
var p, Grid, SearchText, SearchValue, RowIndex : OleVariant;
begin
// Obtain the application process and the grid object
p := Sys.Process('TextFormat');
Grid := p.WinFormsObject('Form1').WinFormsObject('gridControl1');
// Locate the row by cell text
SearchText := aqConvert.DateTimeToFormatStr (aqDateTime.Today(), '%A, %B %d, %Y"');
RowIndex := FindRowByCellText (Grid, 2, SearchText);
if RowIndex <> -1 then
ClickCell (Grid, RowIndex, 2)
else
Log.Error ('Row with text "' + SearchText + '" was not found.');
// Locate the row by cell value
SearchValue := Pi; // 3.14159265358979
RowIndex := FindRow (Grid, 2, SearchValue);
if RowIndex <> -1 then
ClickCell (Grid, RowIndex, 2)
else
Log.Error ('Row with value "' + aqConvert.VarToStr(SearchValue) + '" was not found.');
end;
function FindRow (Grid, ColIndex, Value);
var StartRowIndex, EndRowIndex, i : OleVariant;
begin
// Get indexes of the first and last data rows
StartRowIndex := Grid.GridCellsRange.Top;
EndRowIndex := Grid.GridCellsRange.Bottom;
// Iterate through data rows
for i := StartRowIndex to EndRowIndex do
// Compare the cell value with the specified value
if Grid.Item[i, ColIndex].CellValue = Value then
begin
Result := i; // Row is found;
Exit;
end;
Result := -1; // Row is not found
end;
function FindRowByCellText (Grid, ColIndex, Text);
var StartRowIndex, EndRowIndex, i : OleVaraint;
begin
// Get indexes of the first and last data rows
StartRowIndex := Grid.GridCellsRange.Top;
EndRowIndex := Grid.GridCellsRange.Bottom;
// Iterate through data rows
for i := StartRowIndex to EndRowIndex do
// Compare the cell's display text with the specified string
if Grid.Item[i, ColIndex].FormattedText.OleValue = Text then
begin
Result := i; // Row is found;
Exit;
end;
Result := -1; // Row is not found
end;
procedure ClickCell (Grid, RowIndex, ColIndex);
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, SearchText, SearchValue, RowIndex;
// Obtain the application process and the grid object
p = Sys["Process"]("TextFormat");
Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");
// Locate the row by cell text
SearchText = aqConvert["DateTimeToFormatStr"] (aqDateTime["Today"](), "%A, %B %d, %Y");
RowIndex = FindRowByCellText (Grid, 2, SearchText);
if (RowIndex != -1)
ClickCell (Grid, RowIndex, 2)
else
Log["Error"]("Row with text '" + SearchText + "' was not found.");
// Locate the row by cell value
SearchValue = Math["PI"]; // 3.14159265358979
RowIndex = FindRow (Grid, 2, SearchValue);
if (RowIndex != -1)
ClickCell (Grid, RowIndex, 2)
else
Log["Error"]("Row with value '" + aqConvert["VarToStr"](SearchValue) + "' was not found.");
}
function FindRow (Grid, ColIndex, Value)
{
var StartRowIndex, EndRowIndex, i;
// Get indexes of the first and last data rows
StartRowIndex = Grid["GridCellsRange"]["Top"];
EndRowIndex = Grid["GridCellsRange"]["Bottom"];
// Iterate through data rows
for (i=StartRowIndex; i<=EndRowIndex; i++)
// Compare the cell value with the specified value
if (Grid["Item"](i, ColIndex)["CellValue"]["Equals"](Value))
return i; // Row is found;
return -1; // Row is not found
}
function FindRowByCellText (Grid, ColIndex, Text)
{
var StartRowIndex, EndRowIndex, i;
// Get indexes of the first and last data rows
StartRowIndex = Grid["GridCellsRange"]["Top"];
EndRowIndex = Grid["GridCellsRange"]["Bottom"];
// Iterate through data rows
for (i=StartRowIndex; i<=EndRowIndex; i++)
// Compare the cell's display text with the specified string
if (Grid["Item"](i, ColIndex)["FormattedText"]["OleValue"] == Text)
return i; // Row is found;
return -1; // Row is not found
}
function 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);
}
See Also
Working With Syncfusion GridControl
Accessing Rows, Columns and Cells in Syncfusion GridControl
Iterating Through Rows in Syncfusion GridControl
Obtaining and Setting Cell Values in Syncfusion GridControl