I have a data grid view in my C# Windows Application. I need to change the color of the last 5 characters in a cell but i dont know how to do it.
I have this code in the
You can avoid the background painting code by using the e.PaintBackground
call. Also you have to draw the strings only when the ContentForeGround
is being painted. Use the e.PaintParts
to identify the painting operation. See my sample code for its usage. It needs tweaking but you'll get an idea.
Sample Code:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex != -1 && e.Value != null && e.Value.ToString().Length > 5 && e.ColumnIndex == InterestedColumnIndex)
{
if (!e.Handled)
{
e.Handled = true;
e.PaintBackground(e.CellBounds, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected);
}
if ((e.PaintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None)
{
string text = e.Value.ToString();
string textPart1 = text.Substring(0, text.Length - 5);
string textPart2 = text.Substring(text.Length - 5, 5);
Size fullsize = TextRenderer.MeasureText(text,e.CellStyle.Font);
Size size1 = TextRenderer.MeasureText(textPart1, e.CellStyle.Font);
Size size2 = TextRenderer.MeasureText(textPart2, e.CellStyle.Font);
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
using (Brush cellForeBrush = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.DrawString(textPart1, e.CellStyle.Font, cellForeBrush, rect1);
}
rect1.X += (fullsize.Width - size2.Width);
rect1.Width = e.CellBounds.Width;
e.Graphics.DrawString(textPart2, e.CellStyle.Font, Brushes.Crimson, rect1);
}
}
}