title: DataGridView throws SelectionChanged event for column 0 upon creation
body: <p>I have a base class that extends <em>DataGridView</em>:</p>
  
  <pre><code>public abstract class MyDataGridView : DataGridView
  </code></pre>
  
  <p>I then have a class that extends <em>MyDataGridView</em>. Here is the class definition and the constructor:</p>
  
  <pre><code>// ::FunctionGridView
  public class FunctionGridView : MyDataGridView 
  {
      private static int NUM_COLUMNS = 12;
      private static int NUM_ROWS = 1;
  
      public FunctionGridView()         
      {
          // call the method in the base class to setup the grid
          setupGridView(NUM_ROWS, NUM_COLUMNS);
      }
  }
  </code></pre>
  
  <p>The column type used for my data grid is <em>DataGridViewCheckBoxColumn</em>. Here is the code to setup the columns (defined in the base class):</p>
  
  <pre><code>// ::MyDataGridView
  protected void setupGridView(int numRows, int numColumns)
  {
      for (int i = 0; i &lt; numColumns; i++)
      {
          DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
          column.Name = "Column" + (i + 1).ToString();
          this.Columns.Add(column);
      }
  }
  </code></pre>
  
  <p>I want to catch the <em>SelectionChanged</em> event when the user clicks a cell and then toggle the checkbox for the selected cell. The callback is set in the base class constructor as follows:</p>
  
  <pre><code>// ::MyDataGridView
  // initialize the controls then add the event handler
  public MyDataGridView()
  {            
      initialize();
      this.SelectionChanged += selectionChanged;
  }
  </code></pre>
  
  <p>The selection changed callback is defined as follows:</p>
  
  <pre><code>// ::MyDataGridView
  // get the checkbox _checked_ value and toggle it for the selected cell
  private void handleSelectionChanged(object sender, EventArgs e)
  {
      int currentRow = this.CurrentCell.RowIndex;
      int currentColumn = this.CurrentCell.ColumnIndex;
      bool cellChecked = (bool)this.Rows[currentRow].Cells[currentColumn].Value;
      this.Rows[currentRow].Cells[currentColumn].Value = !cellChecked;
  }
  </code></pre>
  
  <p>Everything works fine except there is one anomaly that I don't know how to handle..</p>
  
  <p>When the program starts and the <em>DataGridView</em> is constructed and added to my <em>Panel</em> the <em>SelectionChanged</em> event is thrown initially for <em>Row[0].Cell[0]</em>. This event is being called even though the user never actually clicked on that cell.</p>
  
  <p>The result is, when my program starts, the first cell gets checked without any user click. My data grid view ends up looking like the following:</p>
  
  <p><img src="https://i144.photobucket.com/albums/r177/tackyjan/datagridview_cell00_checked.png" alt="DataGridView Initial Cell Checked"></p>
  
  <p>Do I need a way to determine if the <em>SelectionChanged</em> callback is being thrown during initialization and <em>not</em> from a user click then exit my callback without toggling the checkbox value?</p>
  
  <pre><code>// ::MyDataGridView
  private void selectionChanged(object sender, EventArgs e)
  {
      **// if (e.isInitializing()) return;**
      ....
      ....
  }
  </code></pre>
  
  <p>I seem to recall in the Java <em>ActionEvent</em> class you can call <em>getSource</em> to make sure the event was triggered by the control and not some other source. Is there something equivalent in C#?</p>
  
  <p><strong>==============================================================================</strong></p>
  
  <p><em>NOTE</em> to keep my question simple I used basic names and left out most of the detail. The real class/base class names are FunctionKeyTimer and KeyTimer (base class). If it helps, here is the class diagram and the full source code:</p>
  
  <p><img src="https://i144.photobucket.com/albums/r177/tackyjan/functiontimer_cd.png" alt="Class Diagram"></p>
  
  <pre><code>public abstract class KeyTimer : DataGridView
  {
      public static int WIDTH = 640;
      public static int HEIGHT = 40;
  
      private bool initialCheckedValue = false;
  
      public KeyTimer()
      {            
          initialize();
          this.SelectionChanged += selectionChanged;
      }
  
      public KeyTimer(bool initialChecked)
      {            
          this.initialCheckedValue = initialChecked;
          initialize();
          this.SelectionChanged += selectionChanged;
      }
  
      protected void setupGridView(int numRows, int numColumns, string columnNamePrefix)
      {
          for (int i = 0; i &lt; numColumns; i++)
          {
              DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
              column.Resizable = DataGridViewTriState.False;
              column.SortMode = DataGridViewColumnSortMode.NotSortable;
              column.Name = columnNamePrefix + (i + 1).ToString();
              this.Columns.Add(column);
          }
  
          for (int i = 0; i &lt; numRows; i++)
          {
              DataGridViewRow row = new DataGridViewRow();
              this.Rows.Add(row);
          }
  
          foreach (DataGridViewRow row in this.Rows)
          {
              for (int i = 0; i &lt; numColumns; i++)
              {
                  row.Cells[i].Value = this.initialCheckedValue;
              }
          }
      }
  
      private void selectionChanged(object sender, EventArgs e)
      {
          int currentRow = this.CurrentCell.RowIndex;
          int currentColumn = this.CurrentCell.ColumnIndex;
          bool cellChecked = (bool)this.Rows[currentRow].Cells[currentColumn].Value;
          this.Rows[currentRow].Cells[currentColumn].Value = !cellChecked;
      }
  
      private void initialize()
      {
          this.AllowUserToDeleteRows = false;
          this.AllowUserToResizeColumns = false;
          ....
          ....
      }
  }
  
  public class FunctionKeyTimer : KeyTimer
  {
      private static int NUM_COLUMNS = 12;
      private static int NUM_ROWS = 1;
      private static string COLUMN_NAME_PREFIX = "F";
  
      public FunctionKeyTimer()         
      {
          setupGridView(NUM_ROWS, NUM_COLUMNS, COLUMN_NAME_PREFIX);
      }
  }
  </code></pre>