简体   繁体   中英

Update Datagridview From Another Form

First I should say i saw this link :

How to refresh datagridview when closing child form?

And i did like this: (i have datagridview in Form1) Form1:

                public void FillDataGridView(DataTable dt1)
            {
                    bindingSource1.DataSource = dt1;
                    bindingSource1.ResetBindings(false);
                    dataGridView1.DataSource = bindingSource1;
                    //here i checked number of rows of dt1 and it shows the correct value
            }

Form2:

           SqlConnection cnn = new SqlConnection(@"My connection string");
            private Form1 Handled_frm1;
            public Form2(Form1 frm1)
            {
                    InitializeComponent();

                Handled_frm1 = frm1;
            }

               private void textbox1_TextChanged(object sender, EventArgs e)
                     {
                        dt.Clear();
                        using (SqlCommand cmd =cnn.CreateCommand())
                        {
                            if (cnn.State == ConnectionState.Closed)
                            {
                                cnn.Open();
                            }
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Connection = cnn;
                            cmd.CommandText = "spSearchCustomerByName";
                            SqlParameter inparam1 = cmd.Parameters.AddWithValue("@name", textbox1.Text);
                            inparam1.Direction = ParameterDirection.Input;
                            dap.SelectCommand = cmd;
                            dap.Fill(dt);

                            Handled_frm1.FillDataGridView(dt);
                        }

But the value Of Datagridview does not change!

Edited:

I wanted to test that if i can clear datagrid view or not,so i changed FillDataGridView like this :

                public void FillDataGridView(DataTable dt1)
            {
                    dt.Clear();
                    dataGridView1.Columns.Clear();
                    dataGridView1.DataSource = null;
                    dataGridView1.Refresh();
            }

but it does not clear datagridview1!!!

I Used mistakenly an incorrect instance of Form1!!

in form1,there is a button that when click it,it shows form2.i wrote this code in click event of it:

Form1 frm1=new Form1();
Form2 frm2=new Form2(frm1);

and this was incorrect,because i made additional instance of Form1.

And I Change the code like this:

Form2 frm2=new Form2(this);

Try using a BindingSource, like described in the following link:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource

I think setting DataSource directly doesn't cause the DataGridView to re-bind, whereas BindingSource takes care of that.

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