对于非静态字段、方法或属性dataGridView1需要一个对象引用。[英] An object reference is required for the non-static field,method, or property dataGridView1

本文是小编为大家收集整理的关于对于非静态字段、方法或属性dataGridView1需要一个对象引用。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我的Windows表单应用程序中有两种形式.我想从第二个形式中获取一些参数,以我的第一个形式过滤DataGridView.

这是我的FilterGrid方法:

public void FilterGrid(string query)
{
    OleDbConnection connection = new OleDbConnection();
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command = new OleDbCommand();

    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tamer Memili\Desktop\Urun.accdb; Persist Security Info=False;";
    connection.Open();

    DataSet dataSet = new DataSet();
    DataTable dataTable = new DataTable();
    dataSet.Tables.Add(dataTable);
    adapter.SelectCommand = command;
    command.Connection = connection;
    command.CommandText = query;

    adapter.Fill(dataTable);
    dataGridView1.DataSource = dataTable.DefaultView;
    connection.Close();
}

我必须将此方法定义为公共静态void才能从form2访问它,但是当我这样做时,我会在此行上出现错误:

dataGridView1.DataSource = dataTable.DefaultView;

我得到此错误:

非静态字段,方法或属性DataGridView1

需要对象引用

我该怎么做?

推荐答案

尝试将FilterGrid方法作为非静态方法:

public void FilterGrid(string query)

然后将Form1引用发送到Form2. 我怀疑您在某个时候做了这样的事情

   Form2 form2 = new Form2()
    form2.showDialog()

您可以将新的构造函数添加到Form2中,看起来像这样

 Form2(Form1 form1Instance)
   {
     this._form1Instance =form1Instance
   }

并将form1的实例传递给form2时2时:

  Form2 form2 = new Form2(this)
  form2.showDialog()

然后,当您想从表单2调用过滤器filterGrid时,您要做的就是

  _form1Instance.FilterGrid("my query")

我希望这会有所帮助

本文地址:https://www.itbaoku.cn/post/607196.html

问题描述

I have two forms in my Windows Forms application. I want to take some parameters from the second form to filter the DataGridView in my first form.

Here is my FilterGrid method on the first form:

public void FilterGrid(string query)
{
    OleDbConnection connection = new OleDbConnection();
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command = new OleDbCommand();

    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tamer Memili\Desktop\Urun.accdb; Persist Security Info=False;";
    connection.Open();

    DataSet dataSet = new DataSet();
    DataTable dataTable = new DataTable();
    dataSet.Tables.Add(dataTable);
    adapter.SelectCommand = command;
    command.Connection = connection;
    command.CommandText = query;

    adapter.Fill(dataTable);
    dataGridView1.DataSource = dataTable.DefaultView;
    connection.Close();
}

I have to define this method as public static void to access it from form2, but when I do that, I get an error on this line:

dataGridView1.DataSource = dataTable.DefaultView;

I get this error:

An object reference is required for the non-static field, method, or property dataGridView1

How can I do that?

推荐答案

Try to make the FilterGrid method as non static :

public void FilterGrid(string query)

Then send the form1 reference to form2. I suspect at some point you do soemthing like this

   Form2 form2 = new Form2()
    form2.showDialog()

You can add a new constructor into Form2 which would look something like this

 Form2(Form1 form1Instance)
   {
     this._form1Instance =form1Instance
   }

and pass the instance of Form1 to Form2 when you construct it:

  Form2 form2 = new Form2(this)
  form2.showDialog()

Then, when you want to call the filter form FilterGrid from form 2 all you do is

  _form1Instance.FilterGrid("my query")

I hope this helps