【问题标题】:Update Datagridview From Another Form从另一个表单更新 Datagridview
【发布时间】:2012-06-08 22:02:23
【问题描述】:

首先我应该说我看到了这个链接:

How to refresh datagridview when closing child form?

我确实是这样的: (我在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:

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

但是Datagridview的值没有变化!

已编辑:

我想测试我是否可以清除数据网格视图,所以我像这样更改了 FillDataGridView:

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

但它并没有清除datagridview1!!!

【问题讨论】:

  • 您的代码没有显示第二种形式的数据与您在第一种形式中使用的数据表之间的关系。例如,cnn 在哪里初始化?
  • 可能是类似类型的 SO 问题 - stackoverflow.com/questions/2947818/… ?
  • @JohnB:cnn 在 Form2 中初始化,我编辑了我的问题
  • Handled_frm1 = fromSearchNewCustomer; 应该是Handled_frm1 = frm1;,否则你只是丢弃了对父表单的引用。然后,当您调用 Handled_frm1.FillDataGridView 时,它会解析为正确的形式。
  • 我在这里发错了,我更新了我的问题

标签: c# winforms datagridview


【解决方案1】:

我误用了 Form1 的错误实例!!

在form1中,有一个按钮,当点击它时,它会显示form2。我在它的点击事件中写了这段代码:

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

这是不正确的,因为我创建了 Form1 的附加实例。

然后我像这样更改代码:

Form2 frm2=new Form2(this);

【讨论】:

    【解决方案2】:

    尝试使用 BindingSource,如以下链接中所述:

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

    我认为直接设置 DataSource 不会导致 DataGridView 重新绑定,而 BindingSource 会处理这个问题。

    【讨论】:

    • 绑定到数据源会自动在控件和相关对象之间设置绑定上下文,因此您的说法是错误的。但是,附加到 BindingSource 确实提供了更多的灵活性,因此仍然有效。
    • 我根据你的链接使用了绑定源,但它并没有解决我的问题,请看我编辑的帖子,我添加了绑定源
    • 尝试调用“bindingSource1.ResetBindings(false);”设置 BindingSource.DataSource 后。另外,不需要每次都设置DataGridView.DataSource,一开始就设置一次。
    • 我做到了,但没有解决!,我添加了一次datagridview数据源
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2015-01-16
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多