Git Product home page Git Product logo

how-to-search-the-whole-word-in-wpf-data-grid's Introduction

How to search the whole word in WPF DataGrid (SfDataGrid)?

About the sample

This sample illustrates how to search the whole word in WPF DataGrid.

WPF DataGrid (SfDataGrid) provides support to search the data by using SearchHelper. Now, the cell values which contains the search text will be highlighted. For example, if the search text is “Alice” and the DataGrid contains the values “Alice” and “Alice Mutton” in two different cells, the search text Alice in both the cells will be highlighted.

You can change this behavior to highlight the cell only if the cell value is completely same as the search text. That is, only the cell with the text “Alice” will be highlighted. This can be achieved by creating a custom SearchHelper and apply Equals search instead of Contains search.

public MainWindow()
{
    InitializeComponent();
    this.dataGrid.SearchHelper = new CustomSearchHelper(this.dataGrid);
}

public class CustomSearchHelper : SearchHelper
{
    public CustomSearchHelper(SfDataGrid dataGrid) : base(dataGrid)
    {

    }

    
    protected override bool ApplyInline(DataColumnBase column, object data, bool ApplySearchHighlightBrush)
    {
        var tempSearchText = SearchText;
        String[] metaCharacters = { "\\", "^", "$", "{", "}", "[", "]", "(", ")", ".", "*", "+", "?", "|", "<", ">", "-", "&" };
        if (metaCharacters.Any(tempSearchText.Contains))
        {
            for (int i = 0; i < metaCharacters.Length; i++)
            {
                if (tempSearchText.Contains(metaCharacters[i]))
                    tempSearchText = tempSearchText.Replace(metaCharacters[i], "\\" + metaCharacters[i]);
            }
        }

        string[] substrings;
        Regex regex;

        if (!AllowCaseSensitiveSearch)
            regex = new Regex("^(" + tempSearchText + ")$", RegexOptions.IgnoreCase);
        else
            regex = new Regex("^(" + tempSearchText + ")$", RegexOptions.None);

        //get all the words from the 'content'

        FrameworkElement columnElement = (FrameworkElement)column.GetType().GetField("columnElement", System.Reflection.BindingFlags.NonPublic | 
            System.Reflection.BindingFlags.Instance).GetValue(column);
        var textBlock = (columnElement as ContentControl).Content as TextBlock;
        textBlock.Inlines.Clear();
        substrings = regex.Split(data.ToString());
        bool success = false;
        foreach (var item in substrings)
        {
            if (regex.Match(item).Success)
            {
                Run run = new Run(item);
                if (ApplySearchHighlightBrush || column.ColumnIndex == CurrentRowColumnIndex.ColumnIndex && column.RowIndex == CurrentRowColumnIndex.RowIndex)
                {
                    if (this.ReadLocalValue(SearchHelper.SearchForegroundHighlightBrushProperty) != DependencyProperty.UnsetValue)
                        run.Foreground = this.SearchForegroundHighlightBrush;
                    run.Background = this.SearchHighlightBrush;
                }
                else
                {
                    if (this.ReadLocalValue(SearchHelper.SearchForegroundBrushProperty) != DependencyProperty.UnsetValue)
                        run.Foreground = this.SearchForegroundBrush;
                    run.Background = this.SearchBrush;
                }
                if (column.GridColumn is GridHyperlinkColumn)
                    textBlock.Inlines.Add(new Hyperlink(run));
                else
                    textBlock.Inlines.Add(run);
                success = true;
            }
            else
            {
                if (column.GridColumn is GridHyperlinkColumn)
                    textBlock.Inlines.Add(new Hyperlink(new Run(item)));
                else
                    textBlock.Inlines.Add(item);
            }
        }
        return success;
    }
}

Searching_Image

KB article - How to search the whole word in WPF DataGrid (SfDataGrid)?

Requirements to run the demo

Visual Studio 2015 and above versions

how-to-search-the-whole-word-in-wpf-data-grid's People

Contributors

9629976110 avatar backiaraj avatar farjanaparveen avatar mohanramanbukkarasu avatar sarubala20 avatar syncsiva avatar vinothkumar-ganesan avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.