ReoGrid
DOCUMENT
Text Search

Overview

ReoGrid provides a built-in text search API that allows you to search for text across cells in one or more worksheets. You can navigate through results one by one, highlight matching cells, or retrieve all matches at once.

The text search classes are located in the unvell.ReoGrid.TextSearch namespace.

using unvell.ReoGrid.TextSearch;

Basic Usage

Search a Single Worksheet

Use TextSearchSession with a single Worksheet to search within it.

var session = new TextSearchSession(grid.CurrentWorksheet, "hello");
session.Search();

// Navigate to the next match
var match = session.NextMatch();
if (match != null)
{
    grid.CurrentWorksheet.SelectionRange = match.Cell.PositionAsRange;
}

Search the Entire Workbook

Pass an IWorkbook instance (the grid control) to search across all worksheets.

var session = new TextSearchSession(grid, "hello");
session.Search();

var match = session.NextMatch();
if (match != null)
{
    match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

Search Specific Worksheets

Pass a list of worksheets to limit the search scope.

var sheets = new List<Worksheet> { grid.Worksheets[0], grid.Worksheets[1] };
var session = new TextSearchSession(sheets, "hello");
session.Search();

After calling Search(), use NextMatch() and PreviousMatch() to navigate through results. Both methods return null when there are no matches.

// Move forward
var match = session.NextMatch();

// Move backward
var match = session.PreviousMatch();

By default, LoopSearch is true, which means navigation wraps around from the last match back to the first (and vice versa). Set it to false to stop at the ends.

session.LoopSearch = false;

var match = session.NextMatch();
if (match == null)
{
    MessageBox.Show("No more results.");
}

Retrieving All Matches

Use the Matches property to access the complete list of results at once.

var session = new TextSearchSession(grid, "hello");
session.Search();

foreach (var match in session.Matches)
{
    Console.WriteLine($"Found at: {match.Cell.Position}, offset: {match.TextIndex}");
}

The ITextSearchMatch interface exposes:

PropertyTypeDescription
CellCellThe cell containing the matched text
TextIndexintThe character position of the match within the cell's display text

Highlighting Matches

Use HighlightTextSearchSession to visually highlight all matching cells.

var session = new HighlightTextSearchSession(grid, "hello", grid.CurrentWorksheet);
session.Search();

// Highlight all matches in gold
session.MarkAllResultHighlight(SolidColor.Goldenrod);

// Navigate
var match = session.NextMatch();
if (match != null)
{
    match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

To remove all highlights (for example, when the search keyword changes):

session.UnmarkAllResultHighlight();

Full Example: Find Dialog

The following example shows how to build a simple find dialog with Next / Previous / Find All functionality.

HighlightTextSearchSession searchSession = null;

HighlightTextSearchSession GetOrCreateSession(string keyword)
{
    if (searchSession == null)
    {
        searchSession = new HighlightTextSearchSession(grid, keyword, grid.CurrentWorksheet);
        searchSession.Search();
        searchSession.MarkAllResultHighlight(SolidColor.Goldenrod);
        searchSession.LoopSearch = true;
    }
    return searchSession;
}

void OnKeywordChanged(string newKeyword)
{
    // Clear highlights from previous session
    searchSession?.UnmarkAllResultHighlight();
    searchSession = null;
}

void FindNext(string keyword)
{
    var match = GetOrCreateSession(keyword).NextMatch();
    if (match != null)
        match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

void FindPrevious(string keyword)
{
    var match = GetOrCreateSession(keyword).PreviousMatch();
    if (match != null)
        match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}

IEnumerable<ITextSearchMatch> FindAll(string keyword)
{
    return GetOrCreateSession(keyword).Matches;
}

API Reference

TextSearchSession

MemberDescription
TextSearchSession(IWorkbook, string, Worksheet?)Search all worksheets in a workbook
TextSearchSession(IEnumerable<Worksheet>, string, Worksheet?)Search the specified worksheets
TextSearchSession(Worksheet, string)Search a single worksheet
void Search()Execute the search. Must be called before navigating results
ITextSearchMatch NextMatch()Returns the next match, or null if none
ITextSearchMatch PreviousMatch()Returns the previous match, or null if none
IEnumerable<ITextSearchMatch> MatchesAll matched results
ITextSearchMatch CurrentMatchGets or sets the current match position
bool LoopSearchWhether to wrap around when reaching the end (default: true)
string TextThe search keyword
List<Worksheet> WorksheetsThe worksheets being searched

HighlightTextSearchSession

Extends TextSearchSession with cell highlighting.

MemberDescription
void MarkAllResultHighlight(SolidColor color)Highlights all matching cells with the specified color
void UnmarkAllResultHighlight()Removes all highlights

Was the content of the page helpful?

© 2012-2026UNVELL Inc.