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 > 1 && 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");
}
}
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...
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
Plz help me the code...
i = dataGridView1.SelectedCells[0]?i = dataGridView1.SelectedCells[0]line.