1

我可以从 datagridview 中删除行,但不能在带有查询代码的 sql 表中。所以基本上我的“字符串查询”代码给出了一个错误。

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");
}
4

1 回答 1

0

我删除了'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");
于 2019-04-17T15:09:15.750 回答