2015-12-14 29 views
8

我有一個小型演示WinForms應用程序。其中一種形式是我的「添加新人員」表單。我使用了詳細信息視圖,而不是使用我的數據源中的DataGridView。當我輸入數據並單擊導航器上的保存按鈕時,零變化,但我在AddNew之後以Load的形式放置MovePreviousMoveNext,一切都按預期工作。爲什麼我需要在更改Binding Source Position之前更改SaveChanges

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位置才能識別更改並保存?

+2

你不需要改變位置,實際上你需要'BindingSource.EndEdit()' –

+2

我想你可以調用'EndEdit()'。 doh,@RezaAghaei 5秒打敗我;) – MickyD

+2

EndEdit的作品。謝謝@RezaAghaei,真的對你們倆! – Randy

回答

3

您不需要更改職位,實際上您需要致電BindingSource.EndEdit,該職位將對基礎數據源應用未決更改。

更改頭寸會導致基礎貨幣經理調用EndCurrentEdit,這就是EndEdit綁定源的方法爲您所做的。

相關問題