Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Highlight only Searched keywords in a Datagrid

Highlight only Searched keywords in a Datagrid

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharpdotnetwpftutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Defender NF
    wrote on last edited by
    #1

    Hi I have a datagird and a searchfunction wich shows only datagridrows contain the searched Keyword. Now i would like to highlight the Searched Keyword in the datagird, i can get acces to the row or cell contains the keyword and also highlight them(for example like this: http://blogs.microsoft.co.il/blogs/tomershamam/archive/2009/08/27/wpf-datagrid-search-and-highlight.aspx ). But how can i highlight only the keyword like google or opera-browser etc.? Thanks very much.

    D 1 Reply Last reply
    0
    • D Defender NF

      Hi I have a datagird and a searchfunction wich shows only datagridrows contain the searched Keyword. Now i would like to highlight the Searched Keyword in the datagird, i can get acces to the row or cell contains the keyword and also highlight them(for example like this: http://blogs.microsoft.co.il/blogs/tomershamam/archive/2009/08/27/wpf-datagrid-search-and-highlight.aspx ). But how can i highlight only the keyword like google or opera-browser etc.? Thanks very much.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You'd have to handle the CellPainting event (.NET 3.0 and above only!) and custom paint every cell in the grid that's being displayed. You'd check to see if the cell contains the search words and custom paint the cell if needed, otherwise let the default painting code render the cell. If you can't custom render the cells, you may want to try just highlighting the individual cells that conatin the search words. This would be done through the CellFormatting event (.NET 2.0 and above).

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You'd have to handle the CellPainting event (.NET 3.0 and above only!) and custom paint every cell in the grid that's being displayed. You'd check to see if the cell contains the search words and custom paint the cell if needed, otherwise let the default painting code render the cell. If you can't custom render the cells, you may want to try just highlighting the individual cells that conatin the search words. This would be done through the CellFormatting event (.NET 2.0 and above).

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        D Offline
        D Offline
        Defender NF
        wrote on last edited by
        #3

        Hi Dave and thanks for the Answer. I dont wanna paint whole the cell but only the keyword. I will handle that with the CollectionInline but i dont know how to seperat the word into normal parts and highlighted part ( keywordpart). Cheers

        D 1 Reply Last reply
        0
        • D Defender NF

          Hi Dave and thanks for the Answer. I dont wanna paint whole the cell but only the keyword. I will handle that with the CollectionInline but i dont know how to seperat the word into normal parts and highlighted part ( keywordpart). Cheers

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          In theory, there's a compule of ways of doing it. First, you have to split the string on the keyword. Say you have a sentance that has one instance of your keyword in it. You split the sentence at the keyword, drawing the first part of the sentance as normal, drawing the keyword part next, then drawing the remaining part of the sentance. The hardest part of this would be tracking exactly where the drawing coordinates would be as you move from segment to segment. The second idea would be to draw the entire sentence, then go back and find the position of each keyword, drawing a transparent highlight box over the parts you want. finding those drawing coordinates would be the difficult part.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            In theory, there's a compule of ways of doing it. First, you have to split the string on the keyword. Say you have a sentance that has one instance of your keyword in it. You split the sentence at the keyword, drawing the first part of the sentance as normal, drawing the keyword part next, then drawing the remaining part of the sentance. The hardest part of this would be tracking exactly where the drawing coordinates would be as you move from segment to segment. The second idea would be to draw the entire sentence, then go back and find the position of each keyword, drawing a transparent highlight box over the parts you want. finding those drawing coordinates would be the difficult part.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            D Offline
            D Offline
            Defender NF
            wrote on last edited by
            #5

            yes thats the difficult part, thats why i m posting here in hope someone has an idea, snippet or something how i can resolve the spliting problem. Cheers

            L D 2 Replies Last reply
            0
            • D Defender NF

              yes thats the difficult part, thats why i m posting here in hope someone has an idea, snippet or something how i can resolve the spliting problem. Cheers

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              There are two possibilities: 1. you use a non-proportional or monospaced font such as Courier New or Consolas; then you can simply count the number of characters, multiply that with an appropriate factor (not an integer!), and use that to calculate your starting and ending positions. The factor depends on font size and more. Either experiment or use #2. (What I do in simple situations is call Graphics.MeasureString once on a string of length 100, then store one hundredth of that as a permanent factor using a float). 2. you use any font you like; with Graphics.MeasureString() you can obtain the width a string will require when going to be painted using Graphics.DrawString() provided you give corresponding parameters; warning: it could be off by a few pixels due to some internal anomalies. If it sounds complex, that is because it is. BTW: there are some good articles that include this subject here at CodeProject, including a recent one; look for things such as "syntax color", "syntax highlight", "TextBox". :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              1 Reply Last reply
              0
              • D Defender NF

                yes thats the difficult part, thats why i m posting here in hope someone has an idea, snippet or something how i can resolve the spliting problem. Cheers

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Like Luc said, this ain't easy at all. I've just scrapped together a test and found it's quite complex. On top of this, you'll have to create your own version of the DataGridViewTextBoxColumn class to even start using this is a DGV. Great, even more complexity! Have you ever created a custom DataGridViewColumn class? Not easy, nor fun! You really have to start asking yourself if this little feature is really worth the effort?? Is it cheaper to possibly find a grid control that already has this capability compared to your cost to develop it?? (Cost to develop = estimated time to complete * what you get paid an hour) Where I work, there are certain people that think internal tools and controls are really cheap to write. Yeah, right... I've got an HTA app (mostly VBScript) that only my team uses. It took me about 3 weeks to write it, costing the company well over $4,000! Again, is it worth the time?

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups