title: How to check if a cell in DataGridView exist?
body: <p>I have a static method that check the sum of an given column. I am using it on event <code>RowsAdded</code>  but I got the following error:</p>
  
  <pre><code>System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  </code></pre>
  
  <p>Here is my code, please tell me where I am wrong</p>
  
  <pre><code>public static decimal datagSum(DataGridView dgv, string colName)
  {
      int i = dgv.RowCount - 1, j = 0;
      decimal dgvSum = 0;
      decimal k = 0;
      while (j &lt; i)
      {
          if (dgv.Rows[(i - 1)].Cells.Count &gt; 3)
          {
  
              if (decimal.TryParse(dgv.Rows[(i - 1)].Cells[colName].Value.ToString(), out k))
              {
                  dgvSum += k;
              }
              j++;
  
          }
      }
      return dgvSum;
  }
  </code></pre>