简体   繁体   English

c# 从另一个用户控件访问和更新 datagridview 数据

[英]c# access and update datagridview data from another user control

I have an application, that has 2 usercontrols我有一个应用程序,它有 2 个用户控件

In the first usercontrol, there is a datagridview and within that, there is a function that gets the data and fills the datagridview.在第一个用户控件中,有一个 datagridview,在其中,有一个 function 获取数据并填充 datagridview。

public void getdata()
        {
            //get data and fill datagrid view...
        }

This function is being called on usercontrol load这个 function 在用户控制负载上被调用


In the 2nd user control, after doing registration, etc. I want to call the getdata() function again so the datagridview is updated in 1st usercontrol.在第二个用户控件中,在进行注册等之后。我想再次调用 getdata() function,以便在第一个用户控件中更新 datagridview。

so this is what I tried:所以这就是我尝试的:

Code_Sign uc = new Code_Sign();
uc.getdata();

//code_sign is name of 1st usercontrol

Doing this, the function is called (I tested this by putting a msgbox in the function. The msgbox is displayed)这样做,function 被调用(我通过在 function 中放置一个 msgbox 来测试这个。显示 msgbox)

However, the problem is this.然而,问题是这样的。 The datagrid view has no changes, eg the new data is not inserted into the datagridview.数据网格视图没有变化,例如新数据没有插入到数据网格视图中。 Same old data is displayed.显示相同的旧数据。

in the getdata() function.在 getdata() function 中。 I have我有

bunifuCustomDataGrid1.Rows.Add(
                new object[]
                {
                    issuer,
                    data,
                    x509.SerialNumber,
                    x509.FriendlyName
                }
                );

(I have also tested this with normal datagridview instead of bunifudatagrid. It doesn't work) (我也用普通的datagridview而不是bunifudatagrid对此进行了测试。它不起作用)

I have set modifier of datagrid view to public but i just can't seem to have it working in any way.我已将 datagrid 视图的修饰符设置为 public,但我似乎无法让它以任何方式工作。

Any help would be appreciate.任何帮助将不胜感激。 I'm not sure what I am missing.我不确定我错过了什么。 Maybe there is another way which I am not aware of.也许还有另一种我不知道的方式。

Thanks in advance提前致谢

Don't create new instance of Code_Sign in 2nd usercontrol.不要在第二个用户控件中创建 Code_Sign 的新实例。 From the 2nd userconntrol you need to get the very first instance of Code_Sign and try to call the getdata() function.从第二个用户控件中,您需要获取 Code_Sign 的第一个实例并尝试调用 getdata() function。

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

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