How do I speed up search in GridDataBoundGrid control?
The GridDataBoundGrid search can be speed up by finding the string through the grid data set records instead loop through the grid cells. The ArrayList maintains the filtered records which has the matching search string. Using the arraylist, you can find the record index of the matched string.
C#
private GridRangeInfo[] FindAllFast(string search, int column)
{
ArrayList searchArray = new ArrayList();
CurrencyManager cm = (CurrencyManager)this.BindingContext[this.gridDataBoundGrid1.DataSource, this.gridDataBoundGrid1.DataMember];
int field = this.gridDataBoundGrid1.Binder.ColIndexToField(column);
for (int i = 0; i < cm.Count; ++i)
{
DataRowView drv = (DataRowView)cm.List[i];
String str = drv.Row[field].ToString();
searchArray.Add(str.ToUpper());
}
ArrayList foundCells = new ArrayList();
try
{
this.Cursor = Cursors.WaitCursor;
int index = searchArray.IndexOf(search.ToUpper());
while (index > -1)
{
foundCells.Add(GridRangeInfo.Cell(index + 1, column));
index = searchArray.IndexOf(search.ToUpper(), index + 1);
}
}
catch { }
finally
{
this.Cursor = Cursors.Default;
}
return (GridRangeInfo[])foundCells.ToArray(typeof(GridRangeInfo));
}
VB
Private Function FindAllFast(ByVal search As String, ByVal column As Integer) As GridRangeInfo()
Dim searchArray As ArrayList = New ArrayList()
Dim cm As CurrencyManager = CType(Me.BindingContext(Me.gridDataBoundGrid1.DataSource, Me.gridDataBoundGrid1.DataMember), CurrencyManager)
Dim field As Integer = Me.gridDataBoundGrid1.Binder.ColIndexToField(column)
For i As Integer = 0 To cm.Count - 1
Dim drv As DataRowView = CType(cm.List(i), DataRowView)
Dim str As String = drv.Row(field).ToString()
searchArray.Add(str.ToUpper())
Next i
Dim foundCells As ArrayList = New ArrayList()
Try
Me.Cursor = Cursors.WaitCursor
Dim index As Integer = searchArray.IndexOf(search.ToUpper())
Do While index > -1
foundCells.Add(GridRangeInfo.Cell(index + 1, column))
index = searchArray.IndexOf(search.ToUpper(), index + 1)
Loop
Catch
Finally
Me.Cursor = Cursors.Default
End Try
Return CType(foundCells.ToArray(GetType(GridRangeInfo)), GridRangeInfo())
End Function
Please refer the sample in the below link which illustrates the above: http://websamples.syncfusion.com//samples/KB/Grid.Windows/Grid_Search/main.htm