c# - Cannot refresh DataGrid in WPF using PRISM 5, MVVM, and EF 6 -
my problem seems common one, yet, of posts have read - both here , on other sites - still have not found solution.
i have simple data-entry wpf module - 2 textboxes, 3 comboboxes, 1 datagrid, , submit , clear buttons. (the app/form creating general ledger accounts in accounting db.) builing entire solution using prism 5. (this first foray remotely complicated, , is, @ moment, proof-of-concept endeavor.) anyway, iam binding wpf screens (usercontrol/views) appropriate viewmodels. viewmodel in turn obtains data msss db via ef 6 entities (db first). when user opens particular wpf screen datagrid shows existing records in database.
with 1 exception submit (new record) procedure works want: new entry pushed through msss db, text boxes cleared, , comboboxes reset. want, however, 1) datagrid refresh, showing new record, , 2) new record highlighted in grid. life of me cannot working, though. note: may make difference datagrid bound view, rather table, in db. (again, datagrid displays correctly when app first opens.)
so, how datagrid update?????
here (condensed) xaml wpf view:
<usercontrol x:class="acctmappingwpfmodule.views.createglacctsview" other namespace declarations... xmlns:vms="clr-namespace:acctmappingwpfmodule.viewmodels"> <usercontrol.datacontext> <vms:createglacctsviewmodel /> </usercontrol.datacontext> ... <stackpanel ...> <!-- layout controls 2 text boxes & 3 comboboxes --> ... <!-- data grid of genl ledger accts --> <datagrid x:name="dgglaccts" isreadonly="true" selectionunit="fullrow" ... itemssource="{binding path=glaccounts}" /> <wrappanel > <!-- submit & clear buttons here --> </wrappanel> </stackpanel> here (condensed) viewmodel code (try/catch blocks omitted):
namespace acctmappingwpfmodule.viewmodels { public class createglacctsviewmodel : bindablebase, icreateglacctsviewmodel { private tbloadentities context; private int _glacctid = 0; // other private fields...
// ctor... public createglacctsviewmodel( ) { this.submitcommand = new delegatecommand(onsubmit); // populate icollectionviews - i.e., properties... using (context = new tbloadentities()) { // 3 properties behind comboboxes populated, datagrid ppt... list<vwglacct> accts = new list<vwglacct>(); accts = (from in context.vwglacct select a).tolist<vwglacct>(); glaccounts = collectionviewsource.getdefaultview(accts); } // hook selection change delegates, including... glaccounts.currentchanged += glaccounts_currentchanged; } private void onsubmit() { glaccount glacct = new glaccount() { // various properties set, then... glfsaccttypecombofk = this.selectedfsaccttypecomboid }; using (context = new tbloadentities()) { context.glacct.add(glacct); context.savechanges(); // vwglacct ef entity of msss db view... list<vwglacct> accts = new list<vwglacct>(); accts = (from in context.vwglacct select a).tolist<vwglacct>(); glaccounts = collectionviewsource.getdefaultview(accts); selectedglacctid = glacct.glacctid; glaccounts.refresh(); } } private void glaccounts_currentchanged(object sender, eventargs e) { vwglacct current = glaccounts.currentitem vwglacct; selectedglacctid = current.glacctid; } public icommand submitcommand { get; private set; } public int selectedglacctid { { return _glacctid; } set { setproperty(ref _glacctid, value); } } public icollectionview glaccounts { get; private set; } } }
1) why getting default view again? (collectionviewsource.getdefaultview(accts)) after submit? make sure accts observablecollection , default view once, hit refresh it.
2) code behind of window? if so, dgglaccts.getbindingexpression(datagrid.itemssourceproperty).updatetarget()
Comments
Post a Comment