为什么我需要更改绑定源位置才能SaveChanges

编程入门 行业动态 更新时间:2024-10-24 18:21:54
本文介绍了为什么我需要更改绑定源位置才能SaveChanges的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个小的演示WinForms应用程序。其中一个表单是我的添加新人表单。我使用数据源中的细节视图而不是codeDataGridView 。当我输入数据并单击导航器上的保存按钮时,会有零个更改,但是我将一个 MovePrevious 和 MoveNext 在 AddNew 之后, Load ,一切都按预期工作。

public partial class AddPersonForm:Form { private readonly DemoContext _context; public AddPersonForm() { _context = new DemoContext(); InitializeComponent(); } protected override void OnLoad(EventArgs e) { _context.People.Load(); personBindingSource.DataSource = _context.People.Local.ToBindingList(); personBindingSource.AddNew(); personBindingSource.MovePrevious(); personBindingSource.MoveNext(); base.OnLoad(e); } private void personBindingNavigatorSaveItem_Click(object sender,EventArgs e) { int changes = _context.SaveChanges(); Debug.WriteLine(#of changes:+ changes); } }

为什么我需要在之前切换BindingSource位置会识别更改并保存?

解决方案

您不需要更改职位,实际上您需要调用 BindingSource .EndEdit 将其应用到基础数据源的更改。

更改职位会导致底层货币管理员调用 EndCurrentEdit ,这就是 EndEdit 绑定源的方法为您而设。

I have a small demo WinForms app. One of the Forms is my Add New Person form. I used the Details View instead of the DataGridView from my Data Sources. When I enter data and click the save button on the Navigator there are zero changes, However I put a MovePrevious and a MoveNext after my AddNew in the form Load, everything works as expected.

public partial class AddPersonForm : Form { private readonly DemoContext _context; public AddPersonForm() { _context = new DemoContext(); InitializeComponent(); } protected override void OnLoad(EventArgs e) { _context.People.Load(); personBindingSource.DataSource = _context.People.Local.ToBindingList(); personBindingSource.AddNew(); personBindingSource.MovePrevious(); personBindingSource.MoveNext(); base.OnLoad(e); } private void personBindingNavigatorSaveItem_Click(object sender, EventArgs e) { int changes = _context.SaveChanges(); Debug.WriteLine("# of changes: " + changes); } }

Why do I need to toggle the BindingSource Position before it will recognize changes and save?

解决方案

You don't need to change the position, in fact you need to call BindingSource.EndEdit that applies pending changes to the underlying data source.

Changing the position causes the underlying currency manager calls EndCurrentEdit and this is what the EndEdit method of binding source does for you.

更多推荐

为什么我需要更改绑定源位置才能SaveChanges

本文发布于:2023-11-13 00:59:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   位置   SaveChanges

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!