title: DataGridView throwing Exception in Default Error Dialog due to DataGridViewImageColumn
body: <p>I am putting this up because it took far too long to find the answer on the web and this is probably a common problem - it is the second time i have experienced it on my app.</p>
  
  <p>When a new row with a DataGridViewImageCell becomes visible, and it has no Default value set, my DataGridView throws the following Exception:</p>
  
  <blockquote>
    <p>The Following Exception occurred in the DataGridView:</p>
    
    <p>System.ArgumentException: Parameter is not valid. at
     System.Drawing.Image.FromStream(Stream stream, Boolean
     useEmbeddedColorManagement, Boolean validateImageData)"</p>
  </blockquote>
  
  <p>In my set up I create the DataGridViewImageColumns in Visual Studio Designer and then bind these columns to DataColumns in a DataTable by setting the DataPropertyName Properties of the DataGridViewImageColumns to match DataColumns of Type: byte[].</p>
  
  <p>However, it still throws this Exception when the DataGridViewImageColumn in the new Row becomes visible.</p>
  
  <p>There are two workarounds that worked for me:</p>
  
  <ol>
  <li>Uncheck the "Enable Adding" option in the Designer - then add rows programmatically - using buttons etc. - I think this is what I did first time round.</li>
  <li><p>Handle the DataError Event of the DataGridView like this:</p>
  
  <pre><code>    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
      {
          if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value)
          {
              e.Cancel = true;
          }
      }
  </code></pre></li>
  </ol>
  
  <p>That's the option I am going with for now but I'm not a fan of suppressing Exceptions and I can see the delay in the creation of the DataGridView row due to throw + Catch by the Handler.</p>
  
  <p>MSDN (<a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx</a>) says that you can handle the RowsAdded event and force a null value.  I tried this:</p>
  
  <pre><code>    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
      {
          foreach (DataGridViewCell cell in dataGridView1.Rows[e.RowIndex].Cells)
          {
              if (cell.GetType() == typeof(DataGridViewImageCell))
              {
                  cell.Value = DBNull.Value;
              }
          }
      }
  </code></pre>
  
  <p>...which didn't work.</p>
  
  <p>The other option involved setting the Column CellTemplate to a Type derived from DataGridViewImageColumn with a default value of null or DBNull.Value.</p>
  
  <p>It's bit late for that now - I've been at this all day.</p>
  
  <p>I'm probably going to go for my option 2, but can anyone tell me how to get option 3/4 to work? Is there a best approach for this?</p>