简体   繁体   English

c#在另一个用户控件中插入数据后刷新另一个用户控件的datagridview

[英]c# refresh datagridview of another usercontrol after inserting data in another user control

I have multiple user control in my c# windows form project.我的 c# windows 窗体项目中有多个用户控件。 Now I only have one form which have multiple buttons each button has corresponding user control.现在我只有一个有多个按钮的表单,每个按钮都有相应的用户控件。 Now the problem is when I inserted a data to database from usercontrol1, I need to view that inserted data in the datagridview of usercontrol2.现在的问题是当我从usercontrol1向数据库插入数据时,我需要在usercontrol2的datagridview中查看插入的数据。

I am assuming, you have a click event handler for Button in UserControl1, which you invoke to insert data to database.我假设您在 UserControl1 中有一个用于 Button 的单击事件处理程序,您可以调用该处理程序将数据插入到数据库中。

Now you have to access the UserControl2 from your parent form, find the DataGridView and do a databind again to get the new data.现在您必须从父窗体访问 UserControl2,找到 DataGridView 并再次进行数据绑定以获取新数据。

I don't have visual studio now, I tried my best to formulate the code here.我现在没有visual studio,我尽力在这里制定代码。

Syntax to Get the Parent Form:获取父表单的语法:

Form parentForm = (this.Parent as Form);

And then find the usercontrol2然后找到usercontrol2

var uc2 = parentForm.Controls.Find("UserControl2", true);

And then find the DataGridView然后找到DataGridView

var dg2 = (DataGridView)parentForm.Controls.Find("datagridview1", true);

And then do a databind然后做一个数据绑定

// Fetch Data from DataBase
dg2.DataBind();

Do you have access to datagridview from userControl2 from your form?您是否可以从表单中的 userControl2 访问 datagridview?
If so you should refresh DataSource after you insert the row.如果是这样,您应该在插入行后刷新 DataSource。
If usercontrol1 and usercontrol2 are custom controls with private implementation you can add event to userControl1 that will be fired when data is saved and method to userControl2 that can refresh DataSource如果 usercontrol1 和 usercontrol2 是具有私有实现的自定义控件,您可以向 userControl1 添加事件,该事件将在保存数据时触发,并将方法添加到可以刷新 DataSource 的 userControl2

public partial class Form1:Form
{
    public Form1()
    {
        InitializeComponent();
        userControl1.DataSaved += (sender, e) => { userControl2.RefreshGrid(); }; // Attach event
    }
}

class UserControl1 : UserControl
{
    public event EventHandler DataSaved;

    private void SaveData() // call this when user saves data
    {
        InsetDataToDb(); // real insert to db
        var handler = DataSaved;
        if (handler != null)
            handler(this, EventArgs.Empty); // call event handler
    }
}
class UserControl2 : UserControl
{
    public void RefreshGrid() 
    {
        // refresh data source of grid view
        dataGridView.DataSource = GetDataSource();
    }
}

In my current control I call this method below to find a correct instance of the target control and refresh the Gridview there.在我当前的控件中,我在下面调用此方法以找到目标控件的正确实例并在那里刷新 Gridview。 To be able to access the target control class I've registered it in my current control.为了能够访问目标控件类,我已经在当前控件中注册了它。 I've also made its refresh datasource methods public inside target control so they can be called from my current control.我还在目标控件中公开了它的刷新数据源方法,以便可以从我当前的控件中调用它们。

public void RefreshGridsAfterAddUpdate()
{
    var ctrlTarget = ((Controls_MyUserControl) Parent.FindControl("myUserControl"));

    if (ctrlTarget != null)
    {
        if (ctrlTarget.PageName == "MyUserControlInstance1")
        {
            var GridView1 = (GridView) ctrlTarget.FindControl("GridView1");
            ctrlTarget.LoadTestingCenter();
            GridView1?.DataBind();
        }else if (ctrlTarget.PageName == "MyUserControlInstance2")
        {
            var GridView2 = (GridView) ctrlTarget.FindControl("GridView2");
            ctrlTarget.LoadTestingCenterStudent();
            GridView2?.DataBind();
        }
    }
}

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

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