Hi,
Here is a sample code. You can try by entering something including "-" in the cell.
example , enter "Color-Test". Then my code will color the string "color" to red and "-Test" to violet.
This is just an example and easy way. You can also create your owm custom cell with your formatting.
Here is code:
private
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.FormattedValue != null)
{
string cellvalue = e.FormattedValue.ToString();
if (cellvalue.Contains("-"))
{
string redvalue = cellvalue.Split('-')[0];
string yellowvalue = "-" + cellvalue.Split('-')[1];
DataGridViewCell cell = (DataGridViewCell)dataGridView1[e.ColumnIndex, e.RowIndex];
if (cell != null)
{
e.PaintBackground(e.ClipBounds,
true);
e.Paint(e.ClipBounds,
DataGridViewPaintParts.Focus);
SizeF redsize = e.Graphics.MeasureString(redvalue, dataGridView1.Font);
Brush redbrush = Brushes.Red;
Brush violetbrush = Brushes.Violet;
e.Graphics.DrawString(redvalue, dataGridView1.Font, redbrush,
new PointF(e.CellBounds.X, e.CellBounds.Y));
e.Graphics.DrawString(yellowvalue, dataGridView1.Font, violetbrush,
new PointF(e.CellBounds.X + redsize.Width + 1, e.CellBounds.Y));
e.Handled =
true;
}
}
}
}
*Don't forget to attach cell_painting to your datagridview.