Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Change text colour only on certain characters within a particular dgv cell

$
0
0

If a cell value contains "1" (among other numbers), I would like to know:

How can I change the colour of that character within that particular dgv cell (or string)?

If a certain condition is met, e.g. a cell have a string like "1,2,3" and contains "1", then I know how to change colour on the whole row where this occures.

What I don't know is how to only change the text colour for the cell, where the condition is met. 
But I want to go further than that, and only change particular characters in the string, and only those containing "1".

How can I acheive that, if possible?

if (dgw != null) { foreach (DataGridViewRow row in dgw.Rows) { //if new row skip, otherwise null reference exception if (row.IsNewRow) return; foreach (DataGridViewCell cell in row.Cells) { if (cell.ColumnIndex == 3) //Particular column index { if (cell.Value.ToString().Contains("1")) { row.DefaultCellStyle.ForeColor = System.Drawing.Color.Red; //INSTEAD CHANGE TEXT COLOUR ON CHARACTERS CONTAINING "1", IN THE PARTICULAR CELL } } } } }





Viewing all articles
Browse latest Browse all 2535

Trending Articles