There is a C # code. It completely fulfills its requirements, but the problem is that the dataGridView is very slow.

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (e.RowIndex < 0 || dataGridView1.Rows[e.RowIndex].IsNewRow) return; var now = DateTime.Now; var s = Regex.CacheSize; now = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0); if (dataGridView1.Rows[e.RowIndex].Cells["Срок до"].Value != null && ((DateTime)dataGridView1.Rows[e.RowIndex].Cells["Срок до"].Value).CompareTo(now) < 0) dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red; for (var i = 0; i < dataGridView1.Rows.Count; i++) for (var j = 0; j < dataGridView1.Columns.Count; j++) { var formattedValue = dataGridView1.Rows[i].Cells[j].FormattedValue; if (formattedValue != null && formattedValue.ToString().ToLower() == "удален") { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray; } } for (var i = 0; i < dataGridView1.Rows.Count; i++) for (var j = 0; j < dataGridView1.Columns.Count; j++) { var formattedValue = dataGridView1.Rows[i].Cells[j].FormattedValue; if (formattedValue != null && formattedValue.ToString().ToLower() == "неограничен") { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow; } } } 

I ask for help with optimization since I am not strong in this matter.

  • Why are you going to cycle through all rows if you only need to decide on the color of a single row? - Pavel Mayorov
  • @PavelMayorov is not no single line. It loads from the database in the dataGridView and should highlight if the expiration date is red, but when comments are added, it changes color - Pavel
  • 2
    You pass GridView twice ( for (var i = 0; i < dataGridView1.Rows.Count; i++) ) why, one thing is enough - Bald
  • @Bald yes. Well, I would be very grateful if you help me to do better. - Pavel
  • one
    Look at the current answer option, remove the line bypass of the grid, due to the fact that your code works out when you redraw the row, the index of the current row can be obtained from e.RowIndex , i.e. enough column walk - Bald

2 answers 2

I would try to rewrite to start:

 var currentRowIndex = e.RowIndex; for (var j = 0; j < dataGridView1.Columns.Count; j++) { var formattedValue = dataGridView1.Rows[currentRowIndex].Cells[j].FormattedValue; var stringValue = formattedValue.ToString().ToLower().Trim(); switch(stringValue) { case "удален": { dataGridView1.Rows[currentRowIndex].DefaultCellStyle.BackColor = Color.Gray; break; } case "неограничен": { dataGridView1.Rows[currentRowIndex].DefaultCellStyle.BackColor = Color.Yellow; break; } default: { //действие по умолчанию break; } } } 

You can also use the conditional if but I prefer the switch

  • @Bald thank you so much. In general, other software began to work, God bless you! - Pavel
  • @ Pavel is not at all, my initial answer would have worked faster than the original one, but with the correction from PavelMayorov I became even better - Bald

What technology are you using? If WinForms, then the DataGridView itself is very long drawn. To work around this problem, you need to create your own control inherited from the DataGridView and set the buffer optimization in it.

 public class ucGridControl : DataGridView { public ucGridControl ( ) : base ( ) { SetStyle ( ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true ); } } 

Accordingly, in the form, create an instance of your class.

  • Is it possible in more detail? - Pavel
  • All figured out. Thank you class very much. It's just a cosmos)))) - Pavel
  • But is it possible not to use the optimization proposed by you without creating a layer in the form of inherited from the base control? - Bald
  • This method has a protected access modifier defined, I could not find any public method to set these parameters. - Trymount