title: DataGridView DefaultCellStyle Preventing BackColor Changes
body: <p>What I am doing is comparing two datatables checking for changes.  If a table cell value changes I mark the corresponding DataGridView cell by changing its BackColor property.  This has worked perfectly until I needed to set the DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True and DataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells.  I needed to do this so I could have text go to a new line inside of a cell.  <a href="https://stackoverflow.com/questions/16514352/multiple-lines-in-a-datagridview-cell">Here</a> is a StackOverflow answer which explains this. So after setting a DefaultCellStyle I was unable to change the BackColor.  However I have a Validator class which gets a DataGridViewCell passed in and validates the value. If the text in the cell is not valid or in correct format the Style.BackColor gets changed...THIS WORKS! My question is why can my Validator class change the BackColor but I can't directly from the form?  Here is a simplified example of what I am doing and what I am experiencing:</p>
  
  <pre><code>    private void initDataGridView()
      {
          DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
          DataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
          DataGridView.MultiSelect = false;
          DataGridView.AllowUserToAddRows = false;
          DataGridView.AllowUserToDeleteRows = false;
          DataGridView.RowHeadersVisible = false;
  
          // ADD EVENT HANDLERS
          DataGridView.CellEndEdit += DataGridView_CellEndEdit;
      }
  
      private void compareTables()
      {
          string dataTableCell;
          string dataTableMirrorCell;
  
          for (int rowIndex = 0; rowIndex &lt; dataTable.Rows.Count; rowIndex++)
          {
              for (int colIndex = 2; colIndex &lt; dataTable.Columns.Count; colIndex++)
              {
                  // initialize cell values to compare
                  dataTableCell = dataTable.Rows[rowIndex].Field&lt;string&gt;(colIndex);
                  dataTableMirrorCell = dataTableMirror.Rows[rowIndex].Field&lt;string&gt;(colIndex);
  
                  // now compare cell values
                  if (dataTableCell != dataTableMirrorCell)
                  {
                      if (dataTableCell== null
                          || dataTableCell== string.Empty)
                      {
                          dataGridView.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Red;
                          dataGridView.Rows[rowIndex].Cells[colIndex].Tag = "Delete";
                      }
                      else
                      {
                          if (dataTableMirrorCell == null
                              || dataTableMirrorCell == string.Empty)
                          {
                              dataGridView.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Orange;
                              dataGridView.Rows[rowIndex].Cells[colIndex].Tag = "Add";
                          }
                          else
                          {
                              dataGridView.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Yellow;
                              dataGridView.Rows[rowIndex].Cells[colIndex].Tag = "Change";
                          }
                      }
                  }
              }
          }
      }
  
      private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
      {
          DataGridView dgv = (DataGridView)sender;
          Validator validator = new Validator(this);
  
          if (e.ColumnIndex != dgv.Columns["Name"].Index)
          {
              DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
  
              // Check the value for valid format
              validator.validateCell(cell, ValidationType.Numeric);               
          }
      }
  </code></pre>
  
  <p>Here is the Validator.validateCell method:</p>
  
  <pre><code>    public void validateCell(DataGridViewCell cell, String type)
      {
          // NUMERIC
          if (type.ToLower() == "numeric")
          {
              int result;
              if (!int.TryParse(cell.Value.ToString(), out result))
              {
                  cell.Style.BackColor = Color.Beige;
                  cell.Style.Font = new Font(this.form.Font, FontStyle.Bold);
                  cell.ToolTipText = "This is a " + type + " only field.";
                  cell.Tag = "invalid";
              }
              else
              {
                  cell.Style.BackColor = Color.White;
                  cell.Style.Font = new Font(this.form.Font, FontStyle.Regular);
                  cell.ToolTipText = string.Empty;
                  cell.Tag = "valid";
              }
          }
      }
  </code></pre>
  
  <p>Validator.validateCell WORKS, it changes the cell BackColor and the Font.  Can someone explain why a local method, compareTables, does not change BackColor and my validator.validateCell does?  Many thanks for helping me learn!</p>