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

查看:629
本文介绍了非静态字段,方法或属性dataGridView1需要对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我必须将此方法定义为public static 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:

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

然后,当您要从表单2调用FilterGrid的过滤器时,您所做的只是

  _form1Instance .FilterGrid(我的查询)

我希望这有助于


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

这篇关于非静态字段,方法或属性dataGridView1需要对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆