从子窗体刷新父窗体datagridview [英] refresh Parent form datagridview from child form

查看:83
本文介绍了从子窗体刷新父窗体datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio 2010中做一个项目

.Net Framework 4.0

问题:

我有两种形式。 Form1 包含 datagridview ,由学生信息填写。 datagridview 旁边有按钮。从 datagrid 中选择一行后,单击按钮。然后我的 Form2 将弹出,它将包含来自 Form1 Datagridvie w选定行的详细信息。在 Form2 这里我可以编辑细节,我可以更新。它也会在我的 sql数据库中发生变化。到这里一切都很好。但当我关闭 Form2 时,我想自动刷新 Form1 datagridview 。它没有发生。我试过很多方面。还没有解决方案。请给我一个想法,正确地做到这一点。在此先感谢。



请帮助我!

I am doing a project in Visual Studio 2010
.Net Framework 4.0
Problem:
I Have two forms. Form1 contains a datagridview which is filled by students Information. next to datagridview there is button. after I select a row from datagrid and I click on the button. then my Form2 will popup and it will contains the details which are from Form1's Datagridview's Selected row. here in Form2 I can Edit the details and I can update. it will changes in my sql database as well. up to here everything is fine. but when I close my Form2 I want to refresh my Form1's datagridview automaticaly. its not happening. I tried in many ways. no solution yet. Please Give me an Idea to do it correctly. Thanks in Advance.

Help me Please!

推荐答案

您好,



关闭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.


这篇关于从子窗体刷新父窗体datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆