How to refresh datagridview when closing child form?

后端 未结 4 1110
南方客
南方客 2020-12-02 17:31

I\'ve a dgv on my main form, there is a button that opens up another form to insert some data into the datasource bounded to the dgv. I want when child form closes the dgv a

4条回答
  •  日久生厌
    2020-12-02 18:08

    We can also proceed this way:

    We have form_1 and form_2

    1. In form_1, create a public method. Inside this public method we put our stuff;
    2. In form_2 we create a global form variable;
    3. Still in form_2, pass form_1 into form_2 through form_2 constructor;
    4. Still in form_2, make your global variable(the one we created in step 2) receive the new form_1 instance we created in form_2 constructor;
    5. Inside the closing_event method we call the method which contains our stuff.

    The method with our stuff is the method that will fill our form1 list, dataGridView, comboBox or whatever we want.

    Form_1:

    public fillComboBox()//Step 1. This is the method with your stuff in Form1
    {
         foreach(var item in collection myInfo)
         {myComboBox.Items.Add(item)}
    
    }
    

    Form_2:

    Form1 instanceForm1;//Step 2
    public Form2(Form1 theTransporter)//Step 3. This the Form2 contructor. 
    { 
        InitializeComponent();
        instanceForm1 = theTransporter;//Step 4
    }
    
    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        instanceForm1.fillComboBox();//Step 5 we call the method to execute the task updating the form1
    }
    

    I hope it helps...

提交回复
热议问题