ORM,数据绑定的DataGridView:插入/删除不保存到数据库中的新行 [英] ORM, DataBinding to DataGridView: inserting/deleted new rows not saved to the database

查看:178
本文介绍了ORM,数据绑定的DataGridView:插入/删除不保存到数据库中的新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的ORM,和我目前给Telerik的OpenAccess的ORM一试,但问题实际上可能不是特定于ORM,我也还没有完全上ORM尚未付清反正。

I'm pretty new to ORMs, and I'm currently giving Telerik 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.

我试图实现是绑定一个DataGridView,显示出在客户表中的所有客户的客户对象集合。

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.

我其绑定到的BindingSource并绑定BindingSource的DataGridView控件。

I've bound it to a BindingSource and bound the BindingSource to the DataGridView control.

我可以修改现有项目成功(使用SaveChanges方法为的OpenAccess ORM)。当我保存的内容保存到数据库如我所料。

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.

不过,如果我从DataGridView中删除行或添加新的,他们不会被保存到数据库中,没有错误消息或异常都没有。

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.

在理想情况下,我想能够执行所有CRUD操作可能与ORM,就像我将能够以一个典型的数据表...

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;



我目前的猜测是,用户添加到DataGridView新行不在列表的一部分但自由站立客户实例?我本来以为他们会自动添加到列表中。同样适用于已删除的行,那我就在想会从列表中自动删除,并从ORM SaveChanges方法将能够挑选呢?

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?

应该我做的东西不仅仅是绑定的更多?
有没有人有任何的成功这样做,而在一般情况下,如何成功的数据绑定与WinForms的经历是,与您选择的ORM(不一定Telerik的)?

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)?

感谢。

推荐答案

您的怀疑是正确的。你正在网格结合对象的自由站立列表,而每个对象是自跟踪,列表是没有的。这就是为什么改变现有对象达到预期效果,但增加/移除没有。

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.

基本例如:

  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();
    }      

这将成为所有ORMS的情况下,据我所知。希望这有助于!

This will be the case for all ORMS, as far as I know. Hope this helps!

问候,

约书亚霍尔特

这篇关于ORM,数据绑定的DataGridView:插入/删除不保存到数据库中的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆