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


i have 2 forms in windows forms application. want take parameters second form filter datagridview in first form.

here filtergrid method on 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 define method public static void access form2, when that, error on line:

datagridview1.datasource = datatable.defaultview; 

i error:

an object reference required non-static field, method, or property datagridview1

how can that?

try make filtergrid method non static :

public void filtergrid(string query) 

then send form1 reference form2. suspect @ point soemthing this

   form2 form2 = new form2()     form2.showdialog() 

you can add new constructor form2 this

 form2(form1 form1instance)    {      this._form1instance =form1instance    } 

and pass instance of form1 form2 when construct it:

  form2 form2 = new form2(this)   form2.showdialog() 

then, when want call filter form filtergrid form 2 is

  _form1instance.filtergrid("my query") 

i hope helps


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -