Home Forums WinForms controls Xceed Grid for WinForms Forecolor only for a part of a cell Reply To: Forecolor only for a part of a cell

User (Old forums)
Member
Post count: 23064

Try this (untested) code:<code>public bool PaintCellValue(GridPaintEventArgs e, Cell cell) {
string value = cell.GetTextToPaint();
if (value.Length == 0) return;
string textRed = “”, textBlue = “”;
int indexDash = value.IndexOf(‘-‘);
if (indexDash < 0) { textRed = value; }
else if (indexDash == 0) { textBlue = value.Substring(1); }
else {
textRed = value.Substring(0, indexDash);
textBlue = value.Substring(indexDash+1);
}
Graphics g = e.Graphics;
Font font = cell.Font;
Rectangle r = e.DisplayRectangle;
r.Inflate(-1, -1);
SizeF sizeRed = g.MeasureString(textRed, font);
SizeF sizeBlue = g.MeasureString(textBlue, font);
SizeF sizeDash = g.MeasureString(” – “, font);
using(SolidBrush brushRed = new SolidBrush(Color.Red)) {
RectangleF rectangleRed = (RectangleF)r;
rectangleRed.Width = sizeRed.Width;
g.DrawString(textRed, font, brushRed, rectangleRed);
}
using(SolidBrush brushDash = new SolidBrush(Color.Black)) {
RectangleF rectangleDash = (RectangleF)r;
rectangleDash.Width = sizeDash.Width;
rectangleDash.Offset(sizeRed.Width + 1f, 0f);
g.DrawString(” – “, font, brushDash, rectangleDash);
}
using(SolidBrush brushBlue = new SolidBrush(Color.Blue)) {
RectangleF rectangleBlue = (RectangleF)r;
rectangleBlue.Width = sizeBlue.Width;
rectangleBlue.Offset(sizeRed.Width + sizeDash.Width + 2f, 0f);
g.DrawString(textBlue, font, brushBlue, rectangleBlue);
}
}</code>

Imported from legacy forums. Posted by Tommy (had 6101 views)