C# Form1 GridView无法通过Form 2按钮访问

C# Form1 GridView无法通过Form 2按钮访问,c#,winforms,gridview,datagridview,C#,Winforms,Gridview,Datagridview,我试图通过Form2中的按钮与位于Form1(frPlanDeLucru)中的DataGridView交互。这样做的原因是创建一个单独的搜索窗口。我不断收到错误CS0122,frPlanDeLucru。由于其保护级别,dataGridView1'无法访问 请帮忙 表格1中的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.

我试图通过Form2中的按钮与位于Form1(frPlanDeLucru)中的DataGridView交互。这样做的原因是创建一个单独的搜索窗口。我不断收到错误CS0122,frPlanDeLucru。由于其保护级别,dataGridView1'无法访问

请帮忙

表格1中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Plan_de_lucru
{
    [Serializable()]
    public partial class frPlanDeLucru : Form
    {
        public frPlanDeLucru()
        {
            InitializeComponent();
        }

        public void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        public void ctrlLoad_Click(object sender, EventArgs e)
        {

            string constr = "Provider = MicroSoft.Jet.OLEDB.4.0; Data Source=" + TextBox1.Text + "; Extended Properties =\"Excel 8.0; HDR=Yes;\";";
            OleDbConnection con = new OleDbConnection(constr);
            OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + textBox2.Text + "$]", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            new Form2().Show();
            this.Show();
        }



            }
        }
格式2中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plan_de_lucru
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void search_button_Click(object sender, EventArgs e)
        {
            String str = "select * from searchBox where ( Name like '%' + @search + '%')";
            BindingSource bs = new BindingSource();
            bs.DataSource = frPlanDeLucru.dataGridView1.DataSource; //<- Here is the problem, do not know the fix



        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间计划
{
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
}
公共无效搜索按钮单击(对象发送者,事件参数e)
{
String str=“从搜索框中选择*,其中(名称类似“%”++@search+'%”);
BindingSource bs=新的BindingSource();

bs.DataSource=frPlanDeLucru.dataGridView1.DataSource;//格式2:

public DataGridView Dgv { get; set; }
Form2 f = new Form2();
f.Dgv = dt; //Add this to the ctrlLoad_Click
格式1:

public DataGridView Dgv { get; set; }
Form2 f = new Form2();
f.Dgv = dt; //Add this to the ctrlLoad_Click

在Form2中访问它自己的Dgv属性。

您可以在创建表单b时添加对表单a的引用,并在该“表单”中公开一个公共方法/属性,该“表单”返回您想要的数据源。使用该方法的示例如下:

public class FormA
{
   public FormA()
   {
   }

   public object GetDataSource()
   {
      return datagrid1.DataSource.ToObject();
   }
}

public class B
{
    private A _formA;

    public B(A formA)
    {
       _formA = formA;
    }

    private void GetDataSourceFromA()
    {
        object dataSource = _formA.GetDataSource();
    }
}

不过,这可能会导致另一个问题;第二个窗体位于不同的线程上。虽然我认为您的问题超出了范围,但从不同的线程调用第一个窗体上的对象是不可能的。

谢谢您的帮助。我发现Form1 GridView设置为private…我在Form1后面输入了代码,并将其修改为私有的
干杯。

这可能也需要委托和线程安全的调用。顺便说一句,
getdatasourceproma
不应该是
private对象而不是
public void
?除非这只是您使用
FormA.GetDataSource()的示例
…Private可以说更好。但是,返回类型取决于您的需要。这只是一个示例。对。这只是一个使用示例。您编写它的方式使它看起来像是机制的一部分。不要在
Form1.Designer.cs
中更改代码,而是在设计模式下打开
Form1
,然后选择gridview和prop在erties窗口中,将
修改器
属性更改为
公共