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");
}
}
這是我從DataGridView和數據庫刪除入圍行,但是當我點擊刪除按鈕,其顯示的錯誤代碼......如何在C#中刪除選定的行從DataGridView和數據庫
「索引超出範圍必須大於集合的大小 非負少參數名:。指數」
plz幫助我的代碼...
拋出的錯誤在哪裏?它在'i = dataGridView1.SelectedCells [0]'? –
此命令引發此錯誤... delcmd.CommandText =「DELETE FROM tb1 WHERE ID =」+ dataGridView1.SelectedRows [i] .Cells [0] .Value.ToString()+「」; – user3138522
你確定嗎?因爲你剛纔評論說它是'i = dataGridView1.SelectedCells [0]'行。 –