How to refresh datagridview when closing child form?

后端 未结 4 1081
南方客
南方客 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 17:58

    Great answer there! The other method would have been:

    1. Check if the form you want to update is open.
    2. Call the method to refresh your gridVieW.

      **inside your refreshMethod() in form1 make sure you set the datasource to null **

     if (System.Windows.Forms.Application.OpenForms["Form1"]!=null)
                {
                    (System.Windows.Forms.Application.OpenForms["Form1"] as Form1).refreshGridView("");
                }

    0 讨论(0)
  • 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...

    0 讨论(0)
  • 2020-12-02 18:21

    There are many ways to do this, but the following is the simpliest and it will do what you want and get you started.

    • Create a public method on your main form.
    • Modified constructor of second form to take a main form.
    • Create an instance of the second form passing the main form object.
    • When closing second form, call the public method of the main form object.

    Form1

    public partial class Form1 : Form {
        public Form1() {
            //'add a label and a buttom to form
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e) {
            Form2 oForm = new Form2(this);
            oForm.Show();
        }
        public void PerformRefresh() {
            this.label1.Text = DateTime.Now.ToLongTimeString();
        }
    }
    

    Form2

    public class Form2 : Form {
        Form1 _owner;
        public Form2(Form1 owner) {
            _owner = owner;
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
            _owner.PerformRefresh();
        }
    }
    
    0 讨论(0)
  • 2020-12-02 18:21

    You're creating a new instance of the main form which isn't effecting the actual main form instance. What you need to do is, call the code on the main form itself, just like the code you say works on the button click:

    private void frmNew_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.itemCategoryBindingSource.EndEdit();
        this.itemsTableAdapter.Fill(myDatabaseDataSet.Items);
        this.dataGridView1.Refresh();
    }
    
    0 讨论(0)
提交回复
热议问题