How to fill a WPF Datagrid but not directly from the DB using C# -


i have been banging head against wall while on one. i'm trying fill wpf datagrid mysql database using c# .net. problem need modify data before sticking datagrid because data not in right format. examples can find when searching how automatically stick data query directly database.

here current table mysql looks like.

1   0456    clockin         5/14/2013 8:36:26 2   0456    breakout    5/14/2013 8:36:39 3   0456    breakin     5/14/2013 8:36:40 4   0456    clockout    5/14/2013 8:36:41 5   0456    clockin         5/14/2013 8:37:14 6   0456    breakout    5/14/2013 8:50:27 7   0456    breakin     5/14/2013 8:50:34 

now need take out punches week(5/12 - 5/18) given userid(0456). need stick values datagrid set 7 days of week. under each day list punches(clockin, breakout, etc) associated times.

edit: tnw asked code. here xml datagrid:

<datagrid autogeneratecolumns="false" height="253" horizontalalignment="center" margin="219,208,0,0" name="weekhours" verticalalignment="top" width="556" horizontalcontentalignment="center" canuseraddrows="true" canuserreordercolumns="false" canuserresizecolumns="false" canuserresizerows="false" canusersortcolumns="false" focusable="false">         <datagrid.columns>             <datagridtextcolumn header="action" width="100"/>             <datagridtextcolumn header="sun" width="65"/>             <datagridtextcolumn header="mon" width="65"/>             <datagridtextcolumn header="tue" width="65"/>             <datagridtextcolumn header="wed" width="65"/>             <datagridtextcolumn header="thu" width="65"/>             <datagridtextcolumn header="fri" width="65"/>             <datagridtextcolumn header="sat" width="65"/>         </datagrid.columns>     </datagrid> 

and code behind, can find on how fill datagrid.

public void filldatagrid(string query, datagrid dg)     {         //open connection         if (this.openconnection() == true)         {             //create command             mysqlcommand cmd = new mysqlcommand(query, connection);             datatable dt = new datatable();             mysqldataadapter da = new mysqldataadapter(cmd);             da.fill(dt);             dg.datacontext = dt;         }     } 

any appreciated. brand new c# , .net. maybe should not using datagrid this. mike

you use view model bound datagrid. when have loaded data database, iterate through records , create view model. following snippet shows way how solve problem:

myviewmodel.cs:

 public class myviewmodel  {       public datetime date { get; set; }       public int punches { get; set; }       public int userid { get; set; }  } 

here bind data datagrid:

 var viewmodels = new list<myviewmodel>();   foreach(var item in dt.rows)  {       var vm = new myviewmodel();       vm.userid = item["userid"];       vm.punches = item["punches"];       vm.date = datetime.parse(item["date"]);       //....       viewmodels.add(vm);  }  dg.datacontext = viewmodels; 

this common pattern if work wpf , maybe mvvm. view model contains actual data optionally modified better displaying in view (here datagrid). if research, notice, view model implements inotifypropertychanged change tracking. view model implement interface, if want react when user modifies in datagrid.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -