c# - How to bind dynamic data to data grid in wpf? -


i have below class datamodel:

public class inspectoroutput {     string m_symbolname;     // each string column name value in double     list<keyvaluepair<string, double>> m_listprices = new list<keyvaluepair<string, double>>();      public string symbolname     {                 {             return m_symbolname;         }          set         {             m_symbolname = value;         }     }      public list<keyvaluepair<string, double>> listprices     {                 {             return m_listprices;         }          set         {             m_listprices = value;         }     }     public void addresult(string strresultname, double nresult)     {         listprices.add(new keyvaluepair<string, double>(strresultname, nresult));     } } 

in xaml window data grid defined below:

<datagrid x:name="gridstockdata">                 </datagrid> 

later on, in mainwindow have below code:

    private void runprofile(object sender, routedeventargs e)     {         observablecollection<inspectoroutput> listoutput = null;         profile.profile objprofile = null;         inspector.inspectormanager objinspectormanager = null;         try         {             // code here makes profile out of user input in objprofile             objinspectormanager = new inspector.inspectormanager();             // calculate data based on given profile             listoutput = objinspectormanager.startinspector(objprofile);             // show calculated data             gridstockdata.itemssource = listoutput;         }         catch (exception ex)         {             logger.getinstance().error(ex.getstacktrace());         }     } 

the problem follows:

  • i have stock data 10 companies.
  • each company has symbol name.
  • calculated data each symbol stored in m_listprices each key column name , each value cell value
    • note: columns not known until run time(ie: based on user's selected algorithm column names , numbers may vary).

i have calculator class runs user selected algorithms. each algorithm has it's own output stores in above data model. how possibly bind datamodel datagrid in wpf?

currently above code gives me following output:

grid output

this how got on problem: create datatable data source data grid filled columns inside dictionary.

                datatable tbl = new datatable();             tbl.columns.add("symbol name", typeof(string));              // add columns             foreach (inspectoroutput item in listscenariooutput)             {                 foreach (keyvaluepair<string, double> entry in item.listprices)                 {                     tbl.columns.add(entry.key, typeof(double));                 }                 break;             }              (int = 0; < listscenariooutput.count; i++)             {                 datarow row = tbl.newrow();                 row.setfield(0, listscenariooutput[i].symbolname);                 int j = 0;                 foreach (keyvaluepair<string, double> entry in listscenariooutput[i].listprices)                 {                     row.setfield(++j, entry.value);                 }                 tbl.rows.add(row);             }              gridstockdata.itemssource = tbl.defaultview; 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -