Adding data separate data grid view

人盡茶涼 提交于 2020-01-17 06:33:16

问题


I want to add data in 2 data gridview but they are in separate form how will i do it? The datagridview1 is in form1 and the datagridview2 is in form2. Because in form1 they cant see the data gridview2 because it is on the other form.


回答1:


You can do it like this ... In form 1 create a property

private static Form1 instance1;
public string propertyForm1;
public static Form1 Instance1
{
    get
    {
        if (instance1 == null)
            instance1 = new Form1();

        return instance1;
    }
}

And, in Form2

private static Form1 instance2;
public string propertyForm2;
public static Form2 Instance2
{
    get
    {
        if (instance2 == null)
            instance2 = new Form2();

        return instance2;
    }
}

Now you can access this property in either of any Form1 or Form2 or any Forms/Classes within your project like this...

Form1.Instance.propertyForm1 = "your assigned value from form2";
Form2.Instance.propertyForm2 = "your assigned value from form1";

Hope this helps you. And, dont forget to mark as answer if this worked for you.



来源:https://stackoverflow.com/questions/41649154/adding-data-separate-data-grid-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!