Highlight text searched programmatically in C1FlexGrid


C1FlexGrid has FlexGridSearchPanel component that lets end-user search for texts in grid and highlights them. However, since it’s a UI component its useful if the developer has his own UI/code to search text and just wants to highlight the searched text in grid.

Steps to Complete:

In C1FlexGrid to highlight text searched through custom code, we should handle its OwnerDrawCell event.

private void C1FlexGrid1_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
    if(!e.Measuring && e.Row > 0 && e.Col > 0 && _searchFilter.Condition1.Parameter!=null)
            using (var backBrush = new SolidBrush(e.Style.BackColor))
                e.Graphics.FillRectangle(backBrush, e.Bounds);
            var text = c1FlexGrid1[e.Row, e.Col].ToString();
            var lastPart = text.Substring(searchedText.Length);
            var point = new PointF(e.Bounds.X, e.Bounds.Y);
            e.Graphics.DrawString(searchedText, e.Style.Font, Brushes.Red, point);
            var rect = e.Graphics.MeasureString(text, e.Style.Font);
            point.X += rect.Width - e.Graphics.MeasureString(lastPart, e.Style.Font).Width;
            using (var foreBrush = new SolidBrush(e.Style.ForeColor))
                e.Graphics.DrawString(lastPart, e.Style.Font, foreBrush, point);
            e.Handled = true;

Ruchir Agarwal