How to RollBack changes to Bound WinForms DataGridView row made in another (details) form when the user presses Cancel button?

I have a Form1 (PatientsMasterForm) which has a DataGridView (patientsDGV) in it.

patientsDGV.DataSource = patientsBindingSource;

patientsBindingSource.DataSource = List<Patient> patients; 

(patients list is returned by a Dapper async query)

When I DoubleClick on a patientsDGV row a second form (PatientDetailsForm) popsup.

On DataGridView cell DoubleClick the patientsBindingSource.Current as Patient is passed as parameter to the second form.

ctor: public PatientDetailsForm (Patient patient) { ... }

This form contains all the Patient details controls, each one of them bound to a local patientBindingSource members.

So:

patientBindingSource.DataSource = patient;

Any changes made to the 2nd form controls contents are simultaneously reflected to the data of the 1st form selected row.

So far so good. If the user presses Save button on the 2nd form all go well.

The problem arises when the user presses the Cancel button of the 2nd form.

The changes made on the 2nd form are already passed to the selected row of the 1st form.

Is there a way of (when user presses Cancel button) reverting the changed contents, of the selected row of the datagridview, to its original (unchanged) data without having to hit the database again requesting the original data?

Thanx indeed for your time



Read more here: https://stackoverflow.com/questions/67384117/how-to-rollback-changes-to-bound-winforms-datagridview-row-made-in-another-deta

Content Attribution

This content was originally published by NAngelGr at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: