且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从子窗体刷新父窗体datagridview

更新时间:2023-12-06 09:58:40

您好,



关闭Form2后,你必须通过这种方式调用Form1_Load(对象发送者,EventArgs e)事件



Hello ,

After closing Form2 , you have to call Form1_Load(object sender, EventArgs e) event by this way

Form1_Load(null,null);





它会自动更新你的datagridview。





问候

Animesh



and It will upate your datagridview automatically.


Regards
Animesh


首先,通常控制如 DataGridView 不需要刷新。您只需更改单元格中的数据,即可刷新视图。 (这是因为属性可以在他们的安装者中设置副作用。)



其次,表格之间没有有效的亲子关系(它仍然可以使用,但不推荐)。如果一个表单是另一个表单的 Owner ,那么这是绝对不同的属性,顺便说一句,你应该总是使用它:它支持同一个应用程序的视图的完整性。此外,一个表单是主表单,一个表单用作 Application.Run 中的参数。无论如何,这些关系与你的应用程序无关。



至于你的特定问题,这是关于表单协作的流行问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
First of all, normally controls like DataGridView don't need "refresh". You just change the data in the cells, and the view is "refreshed". (This is so because properties can have side effects programmed in their setters.)

Secondly, there are no effective parent-child relationships between forms (it can still be used, but it is not recommended). If one form is the Owner of another, this is absolutely different property, which you, by the way, should always use: it supports the integrity of the views of the same application. Besides, one form is a main one, the one used as a parameter in Application.Run. Anyway, these relationships have nothing to do with your application.

As to your particular problem, this is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA





i认为你的问题是自动部分(我是对的吗?)



如果我的回答很简单。

你应该只使用ShowDialog来显示form2用你的按钮。

这个代码的使用是当你关闭form2时,ShowDialog下的代码正在运行所以你只需要编写你的获取数据代码,用它们连接到sql数据库并得到新的show对话框下的数据。
Hi
i think your problem is with automatic part(Am i right?)

if i am your answer is simple.
you should just use ShowDialog to show the form2 with your button.
the use of this code is that when you close form2 the codes under the ShowDialog are running so you just need to write your fetch data code that use them to connect to the sql data base and get the new data under the show dialog.