Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用带有绑定源的winforms,如何知道用户是否修改了记录?_C#_Winforms_Entity Framework_Bindingsource_Bindingnavigator - Fatal编程技术网

C# 使用带有绑定源的winforms,如何知道用户是否修改了记录?

C# 使用带有绑定源的winforms,如何知道用户是否修改了记录?,c#,winforms,entity-framework,bindingsource,bindingnavigator,C#,Winforms,Entity Framework,Bindingsource,Bindingnavigator,我有一个绑定导航器,在winforms窗体上有一个bindingsource。 我的数据源来自实体框架列表 我想使用我从一个网站学到的技术来跟踪绑定源中每个实体的状态 添加和编辑都很简单,我只是填写了添加和删除按钮事件 private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) { ((IEntity)(this.personBindingSource.Current)).St

我有一个绑定导航器,在winforms窗体上有一个bindingsource。 我的数据源来自实体框架列表

我想使用我从一个网站学到的技术来跟踪绑定源中每个实体的状态

添加和编辑都很简单,我只是填写了添加和删除按钮事件

    private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
    {
        ((IEntity)(this.personBindingSource.Current)).State = EntityState.Added;
    }

    private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
    {
        ((IEntity)(this.personBindingSource.Current)).State = EntityState.Deleted;
    }
但是没有EditItemClick事件,如何跟踪实体是否已修改

注意:我绑定到一个断开连接的域类人员列表

 List<Person> people = MyRepository.GetPeople();
 this.personBindingSource.DataSource =people;
 this.personBindingNavigator.BindingSource = this.personBindingSource;
那个人就是从那个里继承下来的 [更新] 查看bindingSource事件属性,我看到在修改属性时会引发CurrentItemChanged。但是,当当前项本身发生更改时,也会引发此问题。
有没有办法确定它是出于什么原因提出的?

由于您的实体已断开连接,因此无法通过上下文进行更改跟踪,因此您需要自己跟踪更改

如果更改存储库方法以创建要检索的人的克隆,我们可以在保存时重新连接原始实体

List<Person> originalPeople;
List<Person> modifiablePeople = MyRepository.GetPeople(out originalPeople);
阅读以下章节:

如何将数据值绑定到绑定导航器中的控件?如果要将实体属性绑定到控件,则只需通过UI更改值即可自动设置EntityState.Modified。如果您绑定正确,实体跟踪器应该会为您处理此问题。谢谢@DavidTansey我更新了问题以显示实体已断开连接。谢谢@user978139,我想一定有办法在修改发生时标记此修改。。。。也许与inotifyChangePropertyNotifyPropertyChanged有关的内容在绑定环境中非常有用,可以通知UI控件更新值。您可以保留一个字符串/布尔键值对字典,其中包含存储库有权访问的每个实体的已修改属性。在存储库中,您将迭代字典并将属性设置为modified context.Entryblog.PropertyName.IsModified=true;这不能回答我的问题。如何知道绑定对象已被修改?
List<Person> originalPeople;
List<Person> modifiablePeople = MyRepository.GetPeople(out originalPeople);
var entry = context.Entry(original Person); 
entry.CurrentValues.SetValues(modifiedPerson);
private void bindingSource_ListChanged(object sender, ListChangedEventArgs e)
    {
        if (e.ListChangedType == ListChangedType.ItemChanged)
        {
            var entity = (IEntity)((BindingSource)sender).Current;
            if (entity.State == EntityState.Unchanged)
            {
                entity.State = EntityState.Modified;
            }
        }
    }