title: How to delete selected row from DataGridView and database in C#
body: <pre><code>private void btnDelete_Click(object sender, EventArgs e)
  {
  
  int i;
  i = dataGridView1.SelectedCells[0].RowIndex;
  
  OleDbConnection con = new OleDbConnection(constr);  
  OleDbCommand delcmd = new OleDbCommand();
  if (dataGridView1.Rows.Count &gt; 1 &amp;&amp; i != dataGridView1.Rows.Count - 1)
  {
        delcmd.CommandText = "DELETE FROM tb1 WHERE ID=" + dataGridView1.SelectedRows[i].Cells[0].Value.ToString() + "";
        con.Open();
        delcmd.Connection = con;
        delcmd.ExecuteNonQuery();
        con.Close();
        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
        MessageBox.Show("Row Deleted");
  }
  
  }
  </code></pre>
  
  <p>this is my code for deleting the seleted row from  datagridview  and database but when i click on the delete button it is showing this error...</p>
  
  <blockquote>
    <p>"Index was out of range. Must be non-negative and less than the size
    of the collection. Parameter name: index"</p>
  </blockquote>
  
  <p>Plz help me the code...</p>