Find-Replace Demo

This sample comes with added support for performing search-and-replace operations with the GridDataBoundGrid control. It allows you to do the following search actions with data records in the grid: find, find all, replace, and replace all. It also explores various search options such as whether to ignore case or perform search only in a current column or whole table.

The grid-find-replace-dialog sink provides the implementation for find-and-replace operations with Grid. It provides the following methods to support search actions:

The search criteria, such as the search string, replace string, and search conditions, can be specified through an instance of the GridFindReplaceEventArgs class. The GridFindTextOptions defines the possible search options for grid data.

The available options are explained below.

Grid Find Text Options

When you run the sample, enter your search string and click FindAll, to find all the occurrences of the given string. The sample will highlight all the data records that contain the search string.

Here is an image showing the sample output.

FindReplaceSample

Below are sample code snippets that implement these operations. The QueryCellStyleInfo event is handled to implement the FindAll operation; it highlights all the data records containing the search string.

Find

	GridFindReplaceEventArgs frEvents = new GridFindReplaceEventArgs(txtSearch.Text, "", searchOptions, locInfo);
	GridFindReplaceDialogSink frDialog = new GridFindReplaceDialogSink(this.gridDataBoundGrid1);
	frDialog.Find(frEvents);

Replace

	GridFindReplaceEventArgs frEvents = new GridFindReplaceEventArgs(txtSearch.Text, txtReplace.Text, searchOptions, locInfo);
	GridFindReplaceDialogSink frDialog = new GridFindReplaceDialogSink(gridGroupingControl1.TableControl);
	frDialog.Replace(frEvents);

Replace All

	GridFindReplaceEventArgs frEvents = new GridFindReplaceEventArgs(txtSearch.Text, txtReplace.Text, searchOptions, locInfo);
	GridFindReplaceDialogSink frDialog = new GridFindReplaceDialogSink(gridGroupingControl1.TableControl);
	frDialog.ReplaceAll(frEvents);

Find All by Query Cell Style Info

	bool findAll = false;
	bool resetAll = false;
	void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
	{
		if (findAll)
		{
			GridCurrentCell gcc = this.gridDataBoundGrid1.CurrentCell;
			if (this.cmbBoxOptions.Text.Equals("WholeTable"))
			{
				SetCellBackColor(e);
			}
			else if (cmbBoxOptions.Text.Equals("ColumnOnly") && gcc!= null)
			{
				if (e.ColIndex == gcc.ColIndex)
				{
					SetCellBackColor(e);
				}
			}
		}
		else if (resetAll)
			e.Style.BackColor = SystemColors.Window;
	}