I'm pretty new to ORMs, and I'm currently giving T开发者_开发技巧elerik OpenAccess ORM a try but the question may actually not be specific to that ORM, and I've not yet completely settled on that ORM yet anyway.
What I'm trying to achieve is to bind a DataGridView to show the collection of Customers objects showing all the customers in the customer table.
I've bound it to a BindingSource and bound the BindingSource to the DataGridView control.
I can modify the existing items successfully (using the SaveChanges method for OpenAccess ORM). When I save, the contents are saved back into the database as I expected.
However, if I delete a row from the DataGridView or add new ones, they are not saved into the database, with no error message or exception at all.
Ideally, I would like to be able to perform all the CRUD operations possible with the ORM, just like I would be able to do this with a typical DataTable...
The code performing the binding looks like this:
List<Customer> ukCustomers = (from c in diagrams.Customer
where c.Country == "UK"
select c).ToList();
customersBindingSource.DataSource = ukCustomers;
customersBindingSource.AllowNew = true;
My current guess is that the new rows added by the user to the DataGridView are not part of the list but "free standing" Customer instances? I would have thought that they would be automatically added to the list. Same goes for deleted rows, that I was thinking would be automatically removed from the list, and that the SaveChanges method from the ORM would be able to pick that up?
Should I be doing something more than just binding? Have anyone had any success doing this, and in general, how successful your data binding experiences with WinForms have been, with your ORM of choice (not necessarily Telerik's)?
Thanks.
You suspicion is correct. You are binding the grid to a "free standing" List of objects, and while each object is self-tracking, the list is not. This is why changes to existing objects work as expected, but adds/removes do not.
One solution is to use an observable collection instead of a standard list. Then you can handle the binding the same, but respond to add/remove events by adding/removing items from the context as needed.
Basic Ex:
private PropertyManagerModel.DemoDBEntityDiagrams context;
public Form1()
{
InitializeComponent();
context = new DemoDBEntityDiagrams();
LoadCommunities();
}
private void LoadCommunities()
{
var communityList = new ObservableCollection<Community>(context.Communities);
communityList.CollectionChanged += new NotifyCollectionChangedEventHandler(communityList_CollectionChanged);
this.dataGridView1.DataSource = new BindingSource() { DataSource=communityList};
}
void communityList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
context.Add(e.NewItems);
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
context.Delete(e.OldItems);
context.SaveChanges();
}
This will be the case for all ORMS, as far as I know. Hope this helps!
Regards,
Joshua Holt
精彩评论