从另一个表单执行表单中的代码 [英] Executing code in a form from another form

查看:73
本文介绍了从另一个表单执行表单中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单form1,它在数据网格视图中显示SQL表中的一些数据。在form1上的菜单工具条中,用户可以发出启动form2的命令。从form2,用户在数据网格视图中的列上输入搜索条件。我希望form2能够执行与form1相同的代码来显示数据网格视图。我该如何做到这一点?



我的尝试:



我还没有尝试过任何东西。

解决方案

最好的方法可能听起来很复杂,但事实并非如此。

创建一个从DataGridView派生的新控件(将UserControl添加到项目中,然后将其从DataGridView派生的内容更改)并添加代码。

然后在Form1和Form2中使用新控件。


表格的数量并不重要!您需要做的就是使用自定义逻辑从Form2获取所有过滤器。如果用户单击[Accept]按钮,则Form1将显示过滤后的数据。



请参阅:

创建Windows表单以搜索数据 [ ^ ]

在表单之间传递数据 [ ^ ]

I have a form, form1, that displays a some data from a SQL table in a data grid view. In a menu tool strip on form1 the user can issue a command that starts form2. From form2 the user enter search criteria on a column in the data grid view. I would like form2 to to execute the same code as form1 to display the data grid view. How do I accomplish this?

What I have tried:

I have not tried anything yet.

解决方案

The best way may sound complicated, but it isn't, not really.
Create a new control derived from DataGridView (add a UserControl to your project, then change what it derives from to DataGridView) and add the code to that.
Then use the new control in both Form1 and Form2.


The number of forms is not important! All what you need to do is to use custom logic to get all filters from Form2. If user click [Accept] button then Form1 will display filtered data.

See:
Create a Windows Form to search data[^]
Pass data between forms[^]


这篇关于从另一个表单执行表单中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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