简体   繁体   English

c#以某种形式调用类函数以从

[英]c# call a class function in a form to change to another from

i have a problem and i need your help. 我有问题,需要您的帮助。 i create 2 windows form and a class in my c# project and i have two dataGridView. 我在我的C#项目中创建了2个Windows窗体和一个类,并且我有两个dataGridView。 one in form 1 and one form 2 with the same name but different content. 一种形式1,另一种形式2具有相同的名称,但内容不同。 i create a class and in that class which called Sqlfunctions i create a SqlConnection and 2 method which called Refresh1 and Refresh2. 我创建一个类,并在该类中调用Sqlfunctions,然后创建一个SqlConnection和2方法,分别调用Refresh1和Refresh2。 I can use each of those method in their respected from by this two line. 在这两行中,我可以使用每种方法。 in form1 在form1中

Sqlfunctions.Refresh(this.dataGridView1);

in form 2 在表格2中

Sqlfunctions.Refresh2(this.dataGridView1);

but i need to call Sqlfunctions.Refresh(this.dataGridView1) in form 2 to update dataGridView1 in form 1. how can i call a class method in one form to update another form. 但是我需要在表单2中调用Sqlfunctions.Refresh(this.dataGridView1)以更新表单1中的dataGridView1。我如何以一种形式调用类方法来更新另一种形式。 or how can i call Sqlfunctions.Refresh(this.dataGridView1) in form 1 when form 2 was closed. 或当窗体2关闭时,如何在窗体1中调用Sqlfunctions.Refresh(this.dataGridView1)。 tank you in advanced 让你前进

here is more information 这是更多信息

i open form 2 with a button in form 1 and i update a dateGridView in form 2. when that happen i want to update dateGridView in form 1 too. 我用窗体1中的按钮打开窗体2,并在窗体2中更新dateGridView。当发生这种情况时,我也想更新窗体1中的dateGridView。 because the dategridview in form 2 and 1 are using same data base. 因为表单2和1中的dategridview使用相同的数据库。 i can update datagridview in form 1 by calling this class function. 我可以通过调用此类函数以表格1更新datagridview。 Sqlfunctions.Refresh(this.dataGridView1); Sqlfunctions.Refresh(this.dataGridView1); but i dont know how to call this class function in form 2 to make change to datagridview in form 1. or even how can i run this method in form 1 when form 2 was closed.both way make datagridview in form 1 to update. 但是我不知道如何在窗体2中调用该类函数以对窗体1中的datagridview进行更改,或者甚至在窗体2关闭时如何在窗体1中运行此方法。两种方式都可以在窗体1中进行datagridview的更新。 please help me. 请帮我。 im stuck. 我卡住了。

i know how to call a method from form 1 when i am in form 2. but the problem is that my method isnt in fomr 1. its in a class called Sqlfunctions 我知道如何在处于表单2中时从表单1调用方法,但是问题是我的方法不在fomr 1中。它在名为Sqlfunctions的类中

i open form 2 with a button in form 1 我用表格1中的按钮打开表格2

Set Form1 as the Owner of Form2 when you open it, like this: 如下所示,将Form1设置为Form2的所有者:

Form2 f2 = new Form2();
f2.Show(this);

Now over in Form2, you can cast the Owner() property to the Form1 type and use it: 现在在Form2中,可以将Owner()属性转换为Form1类型并使用它:

// ... from within Form2 ...
Form1 f1 = (Form1)this.Owner;
Sqlfunctions.Refresh(f1.dataGridView1);

But you would need to select dataGridView1 in Form1 and change its Modifiers() property to Public for this to work. 但是您需要在Form1中选择dataGridView1并将其Modifiers()属性更改为Public才能起作用。

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

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