简体   繁体   English

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

[英]Form1 GridView is not accessible by Form 2 Button

I am trying to interact the DataGridView, located in Form1 (frPlanDeLucru), by a button in Form2.我正在尝试通过 Form2 中的按钮与位于 Form1 (frPlanDeLucru) 中的 DataGridView 进行交互。 The reason for this is to create a separate search window.这样做的原因是要创建一个单独的搜索窗口。 I keep getting Error CS0122, frPlanDeLucru.dataGridView1' is inaccessible due to its protection level.我不断收到错误 CS0122,frPlanDeLucru.dataGridView1' 由于其保护级别而无法访问。

Please help.请帮忙。

The Code in Form 1:表格 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();
        }



            }
        }

The code in Form2: Form2中的代码:

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



        }
    }
}

In Form2:在 Form2 中:

public DataGridView Dgv { get; set; }

In Form1:在 Form1 中:

Form2 f = new Form2();
f.Dgv = dt; //Add this to the ctrlLoad_Click

In Form2 access its own Dgv propety.在 Form2 中访问它自己的 Dgv 属性。

You could add a reference to form a when creating form b and expose a public method / property in that "formA" that returns the datasource you wish.您可以在创建表单 b 时添加对表单 a 的引用,并在该“formA”中公开一个公共方法/属性,以返回您希望的数据源。 An example in which this is used:一个使用它的例子:

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

This might lead to another problem though;不过,这可能会导致另一个问题; the second form being on a different thread.第二种形式在不同的线程上。 Calling objects on the first form from a different thread is not possible, more info here , though I think out of scope for your question.从不同的线程调用第一个表单上的对象是不可能的, 这里更多信息,但我认为超出了您的问题的范围。

Thank you for your help.感谢您的帮助。 I found that Form1 GridView was set to private... i entered in the code behind Form1 and modified it to private.我发现 Form1 GridView 被设置为私有...我在 Form1 后面的代码中输入并将其修改为私有。 Cheers.干杯。

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

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