views:

390

answers:

1

I have a DataGridView control in a winforms app that I'm working on.

The DataSource is set to IQueryable that I get from our service layer:

dataGridView1.DataSource = (from c in _customerService.GetAll() select c);

In the example I'm working on, I have a cancel button that I wish to use to revert changes. If they hit cancel, I do not call DataContext.SubmitChanges() and I close the form. However, when I re-open the form, the data displayed in the DataGridView reflects the changes that I made on the previous form prior to hitting cancel.

When I close the program and run it again, everything's fine. So I believe that I'm changing data in the collection that is bound to the DataGridView.

Surely there is an obvious solution to this and my ignorance of winforms is showing (this is my first stab at anything winforms.)

Any suggestions for reverting changes to a datagridview?

Thanks for the help!

Ian

A: 

I believe you'll have to hit the database again to retrieve the data. Meaning, if you detect that the user decided to cancel their changes, you'll have to go back to the database and get a new IQueryable. Because of the way Linq to Sql works, you can only enumerate the collection once. Therefore, if you want to just revert back to the changes, you can't because it can't enumerate it again.

Personally, I'd probably not use the DataGridView to allow the user to make the changes directly. I've always felt like I'm less in control that way. I'd probably have either some kind of input form where the user can either enter new data or edit existing records, rather then giving them the ability to directly edit the grid which directly edits the database.

BFree