Skip to content

DevExpress-Examples/winforms-grid-change-color-of-highlighted-search-results

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WinForms Data Grid - Change the color of highlighted search results

This example demonstrates how to handle the CustomDrawCell event and paint the highlighted text that matches the search string specified in the grid's Find Panel:

private void OnCustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
    GridView view = sender as GridView;
    if (view.OptionsFind.HighlightFindResults && !view.FindFilterText.Equals(string.Empty)) {
        DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo cellInfo = ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo)e.Cell);
        if (cellInfo != null && cellInfo.ViewInfo != null && cellInfo.ViewInfo.HasMatchedString) {
            e.Appearance.FillRectangle(e.Cache, e.Bounds);                     
            e.Cache.Paint.DrawMultiColorString(e.Cache, e.Bounds, e.DisplayText, cellInfo.ViewInfo.MatchedRanges,
            e.Appearance, e.Appearance.GetStringFormat(), Color.Indigo, Color.LightSlateGray, true);
            e.Handled = true;
        }
    }
}

Another technique to change background and foreground colors of search results is to customize corresponding skin properties. This allows you to change colors of highlighted text across the entire application. Read the following KB article for more information: How to change one skin element in all available skins.

var skin = CommonSkins.GetSkin(LookAndFeel);
skin.Colors[CommonColors.HighlightSearch] = Color.Red;
skin.Colors[CommonColors.HighlightSearchText] = Color.Blue;
Dim skin = CommonSkins.GetSkin(LookAndFeel)
skin.Colors(CommonColors.HighlightSearch) = Color.Red
skin.Colors(CommonColors.HighlightSearchText) = Color.Blue

The following code demonstrates how to obtain these colors:

Color backColor = skin.Colors.GetColor(CommonColors.HighlightSearch);
Color foreColor = skin.Colors.GetColor(CommonColors.HighlightSearchText);
AppearanceDefault highlightAppearance = LookAndFeelHelper.GetHighlightSearchAppearance(LookAndFeel, true);
Dim backColor As Color = skin.Colors.GetColor(CommonColors.HighlightSearch)
Dim foreColor As Color = skin.Colors.GetColor(CommonColors.HighlightSearchText)
Dim highlightAppearance As AppearanceDefault = LookAndFeelHelper.GetHighlightSearchAppearance(LookAndFeel, True)

Note

We recommend that you create a custom skin using our Skin Editor, as skin item names may change in newer versions.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)