title: DataGridView transparent row selection and double click background colour change
body: <p>The following code is designed to setup a DataGridView object in c# .net. It will populate the object and set the 'Selected Cell' fore/back colour to transparent. When a particular row is double clicked, if that row is not set, it should change the background colour to green. If that row is set, it should change the background colour to white.</p>
  
  <p>two problems arise:</p>
  
  <p>1) Initially the first row of the datagrid is highlighted. it is not transparent but solid white. Clicking a different row will fix this and the next selected row is transparent.</p>
  
  <p>2) When a double click has been performed, the datagrid does not change the background of that row to its new colour. It will not change colour unless another row is selected.</p>
  
  <p>I want the background colour change to be instant on the double click operation.</p>
  
  <p>I also want the initial selected row to be transparent.</p>
  
  <p>Can my code be fixed, or is there a better method to attain the desired effect.</p>
  
  <p>Below is a simplified extract of my code.</p>
  
  <p>Create a c# .net project, blank form.</p>
  
  <p>Create a DataGridView object - do not set any parameters via the properties page. Use the following function to be called in the form constructor to setup the table:</p>
  
  <pre><code>const int TotalDummyData = 20;
  
  /// &lt;summary&gt;
  /// 
  /// &lt;/summary&gt;
  private void CreateDGV()
  {
      dgv.Width = 420;
      dgv.Columns.Add("h1", "Name");
      dgv.Columns.Add("h2", "Value");
      dgv.Columns[0].Width = 200;
      dgv.Columns[1].Width = 200;
      dgv.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
      dgv.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
      dgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
      dgv.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
  
      for (int i = 0; i &lt; TotalDummyData; i++)
      {
          DataGridViewRow dgvRow = new DataGridViewRow();
          DataGridViewCell dgvCell1 = new DataGridViewTextBoxCell();
          DataGridViewCell dgvCell2 = new DataGridViewTextBoxCell();
          dgvCell1.Value = "dummy_data_name_" + i.ToString();
          dgvCell2.Value = "dummy_data_value_" + i.ToString();
          dgvRow.Cells.Add(dgvCell1);
          dgvRow.Cells.Add(dgvCell2);
          dgv.Rows.Add(dgvRow);
      }
      dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
      dgv.ColumnHeadersDefaultCellStyle.Font = new Font("Arial", 12.0f, FontStyle.Bold);
      dgv.AllowUserToAddRows = false;
      dgv.AllowUserToDeleteRows = false;
      dgv.AllowUserToResizeColumns = false;
      dgv.AllowUserToResizeRows = false;
      dgv.ReadOnly = true;
      dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
      dgv.RowHeadersVisible = false;
      dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      dgv.DefaultCellStyle.SelectionBackColor = Color.Transparent;
      dgv.DefaultCellStyle.SelectionForeColor = Color.Transparent;
      dgv.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgv_CellContentDoubleClick);
  }
  </code></pre>
  
  <p>Now create the cell double click function - this will change the cell background colour</p>
  
  <pre><code>bool[] DummyDataSet = new bool[20];
  
  /// &lt;summary&gt;
  /// 
  /// &lt;/summary&gt;
  /// &lt;param name ="sender"&gt;&lt;/param&gt;
  /// &lt;param name ="e"&gt;&lt;/param&gt;
  private void dgv_CellContentDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
  {
      if(!DummyDataSet[e.RowIndex])
      {
          DummyDataSet[e.RowIndex] = true;
          dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen;
      }
      else
      {
          DummyDataSet[e.RowIndex] = false;
          dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
      }
  }
  </code></pre>