简体   繁体   English

从另一个表单更新Datagridview

[英]Update Datagridview From Another Form

First I should say i saw this link : 首先,我应该说我看到了这个链接:

How to refresh datagridview when closing child form? 关闭子表单时如何刷新datagridview?

And i did like this: (i have datagridview in Form1) Form1: 我确实喜欢这样:(我在Form1中有datagridview) 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: 窗体2:

           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! 但Datagridview的价值并没有改变!

Edited: 编辑:

I wanted to test that if i can clear datagrid view or not,so i changed FillDataGridView like this : 我想测试一下,如果我可以清除datagrid视图,所以我改变了FillDataGridView,如下所示:

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

but it does not clear datagridview1!!! 但它不清楚datagridview1 !!!

I Used mistakenly an incorrect instance of Form1!! 我错误地使用了Form1的错误实例!!

in form1,there is a button that when click it,it shows form2.i wrote this code in click event of it: 在form1中,有一个按钮,当它单击它时,它显示form2.i在它的click事件中写了这个代码:

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

and this was incorrect,because i made additional instance of Form1. 这是不正确的,因为我做了Form1的额外实例。

And I Change the code like this: 我改变这样的代码:

Form2 frm2=new Form2(this);

Try using a BindingSource, like described in the following link: 尝试使用BindingSource,如以下链接中所述:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource 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. 我认为直接设置DataSource不会导致DataGridView重新绑定,而BindingSource负责处理。

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

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