无法从其他表单刷新datagridview [英] Unable to refresh datagridview from another form

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

问题描述

下午女士和男士。



我有一个可视化的基本MDI表单应用程序,允许用户查看和添加查询。我似乎无法从另一个表单更新一个表单中包含的gridview。



申请表的工作流程如下



工作流程描述



1)父MDI(​​称为'Form1')打开



2)当Form1启动时它打开另一个名为'frmEnquiryBrowser'的表格



3)frmEnquiryBrowser包含当前查询的数据网格视图(称为grdEnquiryView)



4)Form1还包含一个菜单条项,打开一个名为'frmAddEnquiry'的表单



5)用户通过frmEnquiry添加新查询表格。



6)当frmEnquiry保存并关闭时,我希望打开frmEnquiryBrowser gridview'以刷新或更新添加的查询。



我希望这有意义吗?



我尝试了几种方法,



- 将grdEnquiryView刷新方法公开并从frmAddEnquiry调用它

- Instantiatin g frmEnquiryBrowser作为一个表单并尝试以这种方式访问​​它。

- 我可以从Google获取的其他所有内容



似乎因为frmAddEnquiry尚未作为frmEnquiryBrowser的对话框打开我无法更新它的gridview。



任何帮助都会非常感激。

解决方案

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



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



另请参阅:

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

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



但是,我强烈建议不要使用MDI。这是一个想法:谁需要它,永远?为什么要折磨自己并吓跑你的用户?

帮自己一个大忙:根本不要使用MDI。没有它,您可以更轻松地实现设计,质量更好。 MDI甚至被微软高度劝阻,事实上,微软将其从WPF中删除并且很难支持它。更重要的是,如果您使用MDI,您将吓跑所有用户。只是不要。请参阅:

http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [ ^ ],

如何在WPF中创建MDI父窗口? [ ^ ]。



我可以解释做什么。请看我过去的答案:

如何在WPF中创建MDI父窗口? [解决方案2 ],

关于在WPF中使用MDI窗口的问题 [ ^ ],

麦当劳给出错误 [ ^ ],

如何设置子窗体最大化,最后一个子窗体最小化 [ ^ ]。



-SA
< DD> -SA

谢谢!那里有一些非常好的信息。关于vb6,我认为我没有写过桌面应用程序。我有很多需要赶上的。



谢谢!


因为这个问题非常受欢迎,而且我的之前的答案通常都不太清楚,可能还不够清楚,我决定写一篇提示/技巧文章,详细的代码示例和解释:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作



< DD> -SA

Afternoon Ladies and Gents.

I have an visual basic MDI Form Application that allows users to view and add enquiries. I cannot seem to update a gridview contained in one form from another form.

The workflow of the application is as follows

Work Flow Description

1) The parent MDI (called 'Form1') opens

2) When Form1 is initiated it opens another form called 'frmEnquiryBrowser'

3) frmEnquiryBrowser contains a data grid view (called grdEnquiryView) of the current enquiries

4) Form1 also contains a menu strip item that opens a form called 'frmAddEnquiry'

5) The user adds a new enquiry via the frmEnquiry form.

6) When the frmEnquiry saves and closes I wish to have the open frmEnquiryBrowser gridview' to refresh or update with the added enquiry.

I hope this makes sense?

I have tried several methods such,

- Making the grdEnquiryView refresh method public and calling it from frmAddEnquiry
- Instantiating frmEnquiryBrowser as a form and trying to access it this way.
- Everything else I could scrape from Google

It seems that because the frmAddEnquiry has not been opened as a dialogue from frmEnquiryBrowser i cant update it's gridview.

Any help would be much appreciated.

解决方案

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[^].

However, I would strongly advice not to use MDI. Here is the idea: who needs it, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA
—SA


Thanks! Some really good information there. I don't think i have written a desktop app since about vb6. I have a lot to catch up on.

Thanks!


As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


这篇关于无法从其他表单刷新datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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