我在每个论坛上都在搜索,但找不到解决方案。这应该很容易,但它不起作用。
我创建了DataGridView名为“doctorsDataGridView”的以 Doctors Table 作为源。我不想让用户添加项目,但允许删除和编辑。对于删除首先我把这个代码:
private void doctorsDataGridView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
    // Update the balance column whenever rows are deleted.
    try
    {
        SqlCommand cmd = new SqlCommand("DELETE FROM [Doctors] WHERE ([DoctorCode] = @code)", Welcome.con);
        cmd.Parameters.Add("@code", SqlDbType.Int).Value = doctorsDataGridView.Rows[e.RowIndex].Cells["DoctorCode"].Value;
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
        { MessageBox.Show(ex.ToString()); }
}
private void doctorsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    this.Validate();
    this.doctorsBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.clincDataSet);
}
private void AllDoctors_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'clincDataSet.Doctors' table. You can move, or remove it, as needed.
    this.doctorsTableAdapter.Fill(this.clincDataSet.Doctors);
}
但是当我尝试删除任何行时,它只会从数据库中删除,DatagridView而不是从数据库中删除。