void
dataGridView1_CellPainting(
object
sender, DataGridViewCellPaintingEventArgs e)
{
string
kwd =
"关键字"
;
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
e.Handled =
true
;
string
s = e.FormattedValue
as
String;
int
sindx = (s??
""
).IndexOf(kwd);
if
(sindx == -1)
return
;
s = s.Substring(0, sindx);
var g = e.Graphics;
var sz = g.MeasureString(s, e.CellStyle.Font, e.CellBounds.Size,StringFormat.GenericTypographic);
var sz1 = g.MeasureString(kwd, e.CellStyle.Font);
var bound =
new
RectangleF(sz.Width, 0, sz1.Width-2, e.CellBounds.Height);
bound.Offset(e.CellBounds.Location);
var orgColor = e.CellStyle.ForeColor;
e.CellStyle.ForeColor = Color.Red;
var orgClip = g.ClipBounds;
g.SetClip(bound);
e.PaintContent(Rectangle.Round(bound));
e.CellStyle.ForeColor = orgColor;
g.SetClip(orgClip);
}