Highlight the content font of the DataGridView cell in Winform

Source: Internet
Author: User

 

Highlight the content font of the datagridview cell in winform
This is the result of the query function for XXX. When querying keywords, there may be many query results. In order to clearly find the target row from the query result, you can highlight the keyword in the result content to find the target row.
In winform, The datagridview can only control the color of all content in a cell, but cannot control the color of some content. In this case, you can only draw it yourself!
Effect: for example, in Textbox, enter "extended", "extended", and "Xiao yanqiang ".
,,
Enter 154, result

This method is mainly used to trigger the cellpainting event of the datagridview for re-painting. The following two steps are required:
1. Confirm the cell to be repainted
This should be determined based on the specific display requirements, and the cells to be drawn should be determined by logic.
The preceding figure is used as an example. When I enter a number into Textbox, the cells in the "personal ID" column are highlighted. When I enter Chinese characters, the cells in the "name" column are highlighted.
2. redraw Cells
Here we will focus on two steps to redraw a cell:
A. Computing
Because of the convenience of the datagridviewcellpaintingeventargs, the coordinates of cells and other information do not need to be calculated. The location of only the cell content to be calculated and the location of its keyword are also calculated.
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> string cellword = (string) e. Value; & nbsp; // Cell Original Text
String keyword = textBox1.Text. Trim (); & nbsp; // keyword

// *** Calculate the drawing position ***
Rectangle keywordBound = e. CellBounds; & nbsp; // The keyword area. The default value is the cell size.
Rectangle valueBound = e. CellBounds; // The cell content area. The default value is the cell size.
Float fontSizeWeight = 96/(72/e. CellStyle. Font. Size); // actual Font pixel width
Float fontSizeHeight = 96/(72/e. CellStyle. Font. Size); // actual pixel height of the Font
If (IsChinese () & nbsp; // whether it is a Chinese character. if yes, the font width is multiplied by 2.
FontSizeWeight = fontSizeWeight * 2;
Else
Fontsizeweight;

// Coordinates of keywords
Keywordbound. x + = cellword. substring (0, cellword. indexof (keyword). length * (INT) (fontsizeweight/2 );
Keywordbound. Y + = (E. cellbounds. Height-(INT) fontsizeheight)/2;

// Y coordinate of the original text
Valuebound. Y = keywordbound. Y;

B. Draw
Draw by layer, background-> original text-> keywords.
. NET provides us with a convenient paintbackground function, which is used to draw the cell background.
The graphics. drawstring function can be used to conveniently plot the position of the text to achieve the effect.
Code highlighting produced by actipro codehighlighter (freeware) http://www.CodeHighlighter.com/--> // *** draw ***
Using (Brush forecolorbrush = new solidbrush (E. cellstyle. forecolor), keywordcolorbrush = new solidbrush (color. Red ))
{
// Draw the background
E. PaintBackground (e. ClipBounds, false );

// Draw the background (when selected)
If (e. State = (maid. Selected | maid. Displayed | maid. Visible ))
E. PaintBackground (e. ClipBounds, true );

// Draw the original text
E. Graphics. DrawString (cellword, e. CellStyle. Font, foreColorBrush, valueBound, StringFormat. GenericDefault );

// Draw a red keyword on the original text
E. Graphics. DrawString (keyword, e. CellStyle. Font, keywordColorBrush, keywordBound, StringFormat. GenericDefault );

// The event has been processed and the process continues.
E. Handled = true;
}

So far, we can only say that this is not a skill, but a daily solution, but I will share it with you here. If you have any errors or better suggestions, thank you for your guidance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.