Datagridview 行数据删除查询代码不能正常工作

Datagridview row data deletion Query code doesnt work correctly

我可以从 datagridview 中删除行,但不能在 sql 表中使用查询代码。所以基本上我的 'string query ' 代码给出了一个错误。

https://imgur.com/2fn1RCf https://imgur.com/IMc7gBV

private void removebtn_Click(object sender, EventArgs e)
{
     connection.Open();
     int i;
     i = dataGridView1.SelectedCells[0].RowIndex;
     string query = "DELETE FROM PatientRecords WHERE Id=" + dataGridView1.SelectedRows[i].Cells[0].Value.ToString() + "";
     SqlCommand delcmd = new SqlCommand(query, connection);
     delcmd.ExecuteNonQuery();

     dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
     MessageBox.Show("Selected Save Deleted");
}

我删除了“i”变量,并将它们更改为 0,现在代码运行良好,感谢您的帮助

string query = "DELETE FROM PatientRecords WHERE Id=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
        SqlCommand delcmd = new SqlCommand(query, connection);
        delcmd.ExecuteNonQuery();

        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
        MessageBox.Show("Selected Save Deleted");