I'm searching all over every forum but I can't find the solution. It should be easy but it doesn't work.
I have created DataGridView called "doctorsDataGridView" that has Doctors Table as source.
I don't want to allow the user to add items but allow removing and editing. For the deleting first I put this code:
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);
}
But when I try to delete any row it's only deleted from the DatagridView and not the database.
doctorsDataGridView.Rows[e.RowIndex].Cells["DoctorCode"].Valueinto your SQL query in Management Studio and verified the query works directly on the database?DataGridViewand not the database?