c# wpf, trying to load data into a specific forms datagrid from a static method -


i trying load static method data mainwindow-s datagrid (adding columns), trying achieve without creating new instance of window. have hacked way around static limitations fails error

   public partial class mainwindow : metrowindow         {             static mainwindow mainwindow;             public mainwindow()             {              initializecomponent();             argumentloader();             sqlitedatabase.check_for_database();             sqlitedatabase.load_database();             columnloader("alias");          }      public static void columnloader(string alias)     {          datagridtextcolumn textcolumn = new datagridtextcolumn();         textcolumn.header = "x";         textcolumn.binding = new binding("x");         mainwindow.datagrid.columns.add(textcolumn);     } 

exception

an unhandled exception of type 'system.reflection.targetinvocationexception' occurred in presentationframework.dll  additional information: exception has been thrown target of invocation. 

if rename

mainwindow.datagrid.columns.add(textcolumn);  

to

datagrid.columns.add(textcolumn); 

and make method non-static works. missing or there better way ?

you need set mainwindow object instance. can in constructor:

public mainwindow() {     mainwindow = this;      initializecomponent();     argumentloader();     sqlitedatabase.check_for_database();     sqlitedatabase.load_database();     columnloader("alias"); } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -