It is necessary to fill in the entire row for certain text in the dataGridView. I only managed to paint over 1 cell. How to make that would be painted over the whole row?

My code is below.

private void Color() { for (int i = 0; i<dataGridView1.RowCount; i++) { for (int j = 0; j< dataGridView1.Columns.Count; j++) { dataGridView1[j, i].Style.ForeColor = System.Drawing.Color.Black; switch (dataGridView1[j, i].FormattedValue.ToString().ToLower()) { case "удален": dataGridView1[j, i].Style.BackColor = System.Drawing.Color.Green; break; } } } } 
  • so it would be better: switch (dataGridView1 [j, i] .FormattedValue.ToString (). ToLower ()) {case "removed": // ...} - bmo
  • @bmo thank you very much, but the essence of the question remains - Pavel

1 answer 1

Option 1. The value we need in a specific column (for example, in the 2nd):

 private void PaintCell() { for (int i = 0; i < dataGridView1.Rows.Count; i++) { switch (dataGridView1.Rows[i].Cells[1].FormattedValue.ToString()) { case "AAAA": dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; break; default: dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White; break; } } } 

Option 2. The desired value in any column

 private void PaintCell() { for (int i = 0; i < dataGridView1.Rows.Count; i++) for (int j = 0; j < dataGridView1.Columns.Count; j++) { switch (dataGridView1.Rows[i].Cells[j].FormattedValue.ToString()) { case "AAAA": dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; break; } } } 
  • this property needs to be twisted dataGridView1.Rows [i] .DefaultCellStyle.BackColor - bmo
  • @bmo I already have it twisted and in the tail and mane)) - Paul
  • @Pavel did it happen though? - bmo
  • @bmo if (((I would then write a solution ((( - Pavel
  • @MaximK thank you very much. - Pavel