title: Changing row color in not-active DataGridView
body: <p>What's the best way of changing some rows back color in DataGridView, when <strong>it's not active</strong>??</p>
  
  <p>In "real" world I would like to use it for formatting all DataGridView rows after button click, depending on some criterias.</p>
  
  <p>To reproduce behaviour, please try:<br>
  1. In WinForms application put <strong>TabControl</strong> with two tab pages. On the first tab put <strong>button</strong>, on second - <strong>DataGridView</strong>.<br>
  2. Use following code:</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Windows.Forms;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
  
          public int counter = 0;
  
          public Form1()
          {
              InitializeComponent();
  
              DataTable dt = new DataTable();
  
              dt.Columns.Add("Name", typeof(string));
              dt.Columns.Add("Surname", typeof(string));
              dt.Rows.Add("Mark", "Spencer");
              dt.Rows.Add("Mike", "Burke");
              dt.Rows.Add("Louis", "Amstrong");
  
              dataGridView1.DataSource = dt;
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              counter++;
              this.Text = "Event counter: " + counter.ToString();
  
              dataGridView1.Rows[1].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
          }
      }
  }
  </code></pre>
  
  <p>I put the <strong>counter</strong> variable for testing different options to see, how many times the event of color changing is fired (less is better ;) - ideally only once).  </p>
  
  <p>Now, when you first click the button WITHOUT going on tabPage2, and then you switch to tabPage2 - the color of row will not change. This is the problem I have.  </p>
  
  <p>It will work when you <strong>first</strong> activate tabPage2, and <strong>then</strong> press the button, or when you programatically set <code>tabControl1.SelectedIndex = 1;</code>, then color row and then switch back to <code>tabControl1.SelectedIndex = 0;</code> - but in this situation it "blinks".  </p>
  
  <p>I also tried to put code of color changing to <strong>cell_painting</strong> event, but for me it's overkill - it's fired couple hundred times in short time, even when you move mouse over datagridview, while I need to do it only once.  </p>
  
  <p>Do you have any advice how to solve that issue?</p>
  
  <p>Best regards,<br>
  Marcin</p>