title: Deleting ALL selected rows from datagridview
body: <p>I am using datagridview to view a SQL Table on Windows Form and I was hoping to use this to be able to add/delete rows from the SQL table.</p>
  
  <p>I am using the following code to delete the selected rows from the datagridview:</p>
  
  <pre><code>        try
          {
              int count = dataGridView.SelectedRows.Count;
  
              foreach (DataGridViewRow item in dataGridView.SelectedRows)
              {
                  TA.DeleteQuery(item.Cells[0].Value.ToString());
              }
          }
          catch (Exception ex) { Logs.LogEvent(3, ex.Message + "\n" + ex.ToString()); }
          finally { UpdateTable(); }
  </code></pre>
  
  <p>This code deletes all rows except the first row that is selected. When I step through the code the dataGridView selected rows count keeps displaying one less than the value selected which is what I expected but the foreach loop only iterates through 3 times. When I used a for loop to iterate through 4 times it gave me an out of range error!</p>
  
  <p>Have you guys any idea why this might be and if there is a better way to approach this?</p>
  
  <p>Let me know if you need more details.</p>
  
  <p>Thanks in advance guys!</p>