Deleting Cars
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
You will now add functionality to MainForm to delete existing cars.
-
In the code behind of MainForm define a new property SelectedCar. It will be used to retrieve the car which is currently selected in the RadGridView control of the form.
private Car SelectedCar { get { return this.carsBindingSource.Current as Car; } }
Private ReadOnly Property SelectedCar() As Car Get Return Me._carsBindingSource.Current End Get End Property
-
In the Design view of MainForm double click the Delete button to generate its Click event handler. Here a confirmation dialog is shown and depending on the result from it the delete operation is performed. If the deletion is successful, the data displayed in the grid is refreshed by the PopulateBindingSource() method, if not a message is shown.
private void rbtnDeleteCar_Click(object sender, EventArgs e) { Car selectedCar = this.SelectedCar; if (selectedCar == null) { RadMessageBox.Show("You must first select a car in order to delete it!", "Warning", MessageBoxButtons.OK, RadMessageIcon.Exclamation); return; } DialogResult deleteConfirmation = RadMessageBox.Show(string.Format("You are about to delete {0} {1}. Are you sure?", selectedCar.Make, selectedCar.Model), "Confirm delete", MessageBoxButtons.YesNo, RadMessageIcon.Question); if (deleteConfirmation != DialogResult.Yes) { return; } this.context.Delete(selectedCar); bool saveChangesIsSuccessful = false; try { this.context.SaveChanges(); saveChangesIsSuccessful = true; } catch (DataStoreException ex) { RadMessageBox.Show("Could not delete this car entry" + "because other entries related to it" + " exist in the database. Exception message: \n\r" + ex.BackendError.Description, "Error", MessageBoxButtons.OK, RadMessageIcon.Error); } catch (Exception ex) { RadMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, RadMessageIcon.Error); } if (saveChangesIsSuccessful) { this.PopulateBindingSource(); } }
Private Sub rbtnDeleteCar_Click(sender As Object, e As EventArgs) Handles rbtnDeleteCar.Click Dim selectedCar As Car = Me.SelectedCar If IsNothing(selectedCar) Then RadMessageBox.Show("You must first select a car in order to delete it!", "Warning", MessageBoxButtons.OK, RadMessageIcon.Exclamation) Exit Sub End If Dim deleteConfirmation As DialogResult = RadMessageBox.Show(String.Format("You are about to delete {0} {1}. Are you sure?", selectedCar.Make, selectedCar.Model), "Confirm delete", MessageBoxButtons.YesNo, RadMessageIcon.Question) If deleteConfirmation <> DialogResult.Yes Then Exit Sub End If Me._context.Delete(selectedCar) Dim saveChangesIsSuccessfull As Boolean = False Try Me._context.SaveChanges() saveChangesIsSuccessfull = True Catch ex As DataStoreException RadMessageBox.Show("Could not delete this car entry" + " because other entries related to it" + " exist in the database. Exception message:" + vbCrLf + ex.BackendError.Description, "Error", MessageBoxButtons.OK, RadMessageIcon.Error) Catch ex As Exception RadMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, RadMessageIcon.Error) End Try If saveChangesIsSuccessfull Then Me.PopulateBindingSource() End If End Sub
Checkpoint
You can now delete existing cars through MainForm.