Highlight only Searched keywords in a Datagrid
-
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.
-
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.
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 -
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 KreskowiakHi 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
-
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
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 -
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 Kreskowiakyes 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
-
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
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.
-
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
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