简体   繁体   English

c#Winform更新表格的最佳方法?

[英]c# Winform best way to update form?

I am trying to update a selected record using this code when closing the form: 关闭表单时,我尝试使用此代码更新所选记录:

interestsTableAdapter.Update(newCityCollectionDataSet);

The record is NOT updating doing this. 该记录不会更新。 I use the same code to update datagridviews but I do it while it is validating. 我使用相同的代码来更新datagridviews,但是我正在验证它。 Is there a better way to do this with FORMS as opposed to controls? 与控件相比,是否有更好的方法来使用FORMS?

Why this isn't working could depend on when you're trying to save your record. 为什么此方法不起作用可能取决于您尝试保存记录的时间。 There's a big difference between executing code before your form's closing and while your form's closing. 有你的窗体的收盘表单收盘执行代码之间有很大的区别。

Typically, failures while the form's closing (in handlers of a Form's FormClosed event) are caused by the application exiting before the handler gets a chance to do its job. 通常, 窗体关闭失败(在窗体的FormClosed事件的处理程序)是由退出处理程序有机会完成其工作之前,应用程序引起的。

On the other hand, handlers of a Form's FormClosing event are invoked before the form even starts to close, and can even keep it from closing. 另一方面,在窗体甚至开始关闭之前 ,就调用了Form的FormClosing事件的处理程序,甚至可以阻止它关闭。 These handlers are almost always waited for, as long as the form is the application's "main" or "startup" form (and not a child of it). 只要表单是应用程序的“主”或“启动”表单(而不是其子表单),几乎总是等待这些处理程序。

Without seeing more code than what you've provided, I'm afraid it's all I can do to advise you to execute your code before the form closes (via its FormClosing event), rather than while it's closing (via its FormClosed event). 如果看不到比提供的代码更多的代码,恐怕我只能建议您在关闭窗体之前(通过其FormClosing事件)而不是在关闭窗体时(通过其FormClosed事件)执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM