This sample implements the find-replace functionality in a grid control. It makes use of the built-in GridFindReplaceDialogSink and GridFindReplaceDialogEventArgs helper classes to construct the custom find-replace dialog box. The former class constructs a find-replace dialog box sink wherein the options for the find-replace operation are specified through an instance of the latter class. FindReplaceDialogSink, which has been constructed as mentioned above, will then be associated with the grid control. The GridFindReplaceDialogSink class provides the methods that are necessary to perform a find-replace operation.
Features
Search
The options for searching the grid for a given text can be specified in terms of
the GridFindTextOptions enumeration. The possible options available are:
Find Next
This option searches for text, and moves the current cell position
to the cell that contains the search text. Clicking the Find Next button, navigates through the destination cells
one by one.
GridFindTextOptions options = GridFindTextOptions.WholeTable | GridFindTextOptions.SearchUp; object locInfo = GridRangeInfo.Table(); GridFindReplaceEventArgs frEvents = new GridFindReplaceEventArgs(cmbSearch.Text, "", options, locInfo); GridFindReplaceDialogSink frDialog.Find(frEvents);
Find All
This option loops through all the cells in the grid to search for the given text and highlights the cells that contain the search text.
GridFindTextOptions options = GridFindTextOptions.WholeTable | GridFindTextOptions.SearchUp; object locInfo = GridRangeInfo.Table(); GridRangeInfo selRange = GridRangeInfo.Empty; int rowIndex, colIndex; if ((options & GridFindTextOptions.SelectionOnly) != GridFindTextOptions.None) selRange = gridControl1.Selections.Ranges.ActiveRange; else if ((options & GridFindTextOptions.ColumnOnly) != GridFindTextOptions.None) selRange = GridRangeInfo.Col(gridControl1.CurrentCell.ColIndex); else if ((options & GridFindTextOptions.WholeTable) != GridFindTextOptions.None) selRange = GridRangeInfo.Cells(1, 1, gridControl1.RowCount, gridControl1.ColCount); int startTop = selRange.Top; int startLeft = selRange.Left; while (GridFindReplaceDialogSink.GetNextCell(selRange, ref startTop, ref startLeft, false, chkSearchUp.Checked)) { GridStyleInfo style = gridControl1[startTop, startLeft]; GridCellRendererBase renderer = gridControl1.CellRenderers[style.CellType]; if (renderer.FindText(cmbSearch.Text, startTop, startLeft, options, true)) { gridControl1.CurrentCell.GetCurrentCell(out rowIndex, out colIndex); gridControl1[rowIndex, colIndex].BackColor = Color.Orange; } }
Replace
This option replaces text, one at a time.
GridFindTextOptions options = GridFindTextOptions.WholeTable | GridFindTextOptions.SearchUp; object locInfo = GridRangeInfo.Table(); GridFindReplaceEventArgs frEvents = new GridFindReplaceEventArgs(cmbSearch.Text, cmbReplace.Text, options, locInfo); GridFindReplaceDialogSink frDialog.Replace(frEvents);
Replace All
This option replaces text in all the applicable cells at once.
GridFindTextOptions options = GridFindTextOptions.WholeTable | GridFindTextOptions.SearchUp; object locInfo = GridRangeInfo.Table(); GridFindReplaceEventArgs frEvents = new GridFindReplaceEventArgs(cmbSearch.Text, cmbReplace.Text, options, locInfo); GridFindReplaceDialogSink frDialog.ReplaceAll(frEvents);
The following illustration shows the find-replace options of grid.