简体   繁体   中英

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

I have multiple user control in my c# windows form project. 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.

I am assuming, you have a click event handler for Button in UserControl1, which you invoke to insert data to database.

Now you have to access the UserControl2 from your parent form, find the DataGridView and do a databind again to get the new data.

I don't have visual studio now, I tried my best to formulate the code here.

Syntax to Get the Parent Form:

Form parentForm = (this.Parent as Form);

And then find the usercontrol2

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

And then find the 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?
If so you should refresh DataSource after you insert the row.
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

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. 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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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