Wednesday, 15 April 2015

wpf - Raise an event in datagridview to update database for delete or update MVVM method -



wpf - Raise an event in datagridview to update database for delete or update MVVM method -

i think missing simple doing multiple step method data. allow me run through simpler version of doing in example.

i have observable collection implements inotifypropertychanged

the observable collection of class 'poco' simple poco class makes these 2 properties:

personid int { get; set; } name string { get; set; }

i have entity sql info model maps simple database table contains same meta values in poco class , let's simple illustration has 3 row values:

personid, name 1, brett 2, emily 3, test

the observable collection wired in modelview so:

observablecollection<poco> _pocos; pocoentities ee = new pocoentities(); public observablecollection<poco> pocos { { if (_pocos == null) { list<poco> mes = this.getpocos(); _pocos= new observablecollection<poco>(mes); } homecoming _pocos; } set { _pocos = value; onpropertychanged("pocos"); } } list<poco> getpocos() { homecoming ee.vpoco.select(p => new pocoview() { personid = p.personid, name = p.name }).tolist(); }

i have current item wired such.

poco _currentpoco; public poco currentpoco { { homecoming _currentpoco; } set { _currentpoco = value; onpropertychanged("currentpoco"); } }

4 , 5 guts of modelview wire them view of datagrid such:

<datagrid x:name="datagrid" itemssource="{binding pocos}" currentitem="{binding currentpoco}" />

this part not get, how update database's entity model in near real time? collection wired fine , updating, how tell database happened? if set event 'celleditending' or 'selectionchanged' , seek implement update proc entity model bombs in modelview. if stick code behind works, kind of, not seem capture 'after' changed value.

even using icommand property , implementing relay command done in mvvm. these methods won't work. curious if on thinking , type of interface can bake in refreshing database you. can handle inserting docs , using method populate or refresh datagrid able alter values in datagridview , update database directly.

summary: in simplest way possible wanting update database alter datagridview , observablecollection changes 2 sync each other.

there 2 categories of changes here:

the collection changes, i.e. items added and/or removed. track these changes, create vm hear observablecollection's collectionchanged event , utilize newitems , olditems properties figure out info add together and/or remove db. properties on 1 of poco instances changes, e.g. alter name of person. alter not trigger collectionchanged event collection still same.

for #2 implement simple viewmodel poco class handles updates properties. after all, poco should considered business object , should not exposed view directly. each pocovm holds reference single poco instance.

edit

i added more or less of code used in experiement, except stubbed database since have no thought using , how works. doesn't matter long returns lists of items , can tell update single item.

xaml same yours except added grid (readonly) show changes 1 time got accepted mysticaldblayer. got rid of currentitemas using pocovm maintain track of item editing.

<grid> <grid.rowdefinitions> <rowdefinition /> <rowdefinition height="auto"/> <rowdefinition /> <rowdefinition height="auto"/> </grid.rowdefinitions> <textblock>input-grid</textblock> <datagrid grid.row="1" itemssource="{binding pocos}"/> <textblock grid.row="2">readonly-grid</textblock> <datagrid grid.row="3" itemssource="{binding pocos, mode=oneway}" isreadonly="true"/> </grid>

view model (datacontext of xaml) goes file. database connection might vary based on use, have observable collection populate same way do, except create new pocovm (viewmodel) every poco and add together new pocovm observablecollection instead of poco itself.

class vm { observablecollection<pocovm> _pococollection = new observablecollection<pocovm>(); public observablecollection<pocovm> pocos { { if (_pococollection.count == 0) { _pococollection = new observablecollection<pocovm>(); ienumerable<poco> pocos = mysticaldblayer.getitems(); foreach (poco poco in pocos) { _pococollection.add(new pocovm(poco)); } } homecoming _pococollection; } } }

and pocovm every time seek update value of 1 of cells (only name update:able code number has getter), corresponding setter called in class. here can write db , deed based on whether worked out or not.

class pocovm : inotifypropertychanged { private poco _datainstance = null; public pocovm(poco datainstance) { _datainstance = datainstance; } public uint number { { homecoming _datainstance.number; } } public string name { { homecoming _datainstance.name; } set { if (string.compare(value, _datainstance.name, stringcomparison.currentcultureignorecase) == 0) return; if (!mysticaldblayer.updatepoco(_datainstance, new poco(_datainstance.number, value))) return; _datainstance.name = value; onpropertychanged("name"); } } public event propertychangedeventhandler propertychanged; void onpropertychanged(string property) { if (propertychanged == null) return; propertychanged(this, new propertychangedeventargs(property)); } }

wpf entity-framework mvvm datagridview

No comments:

Post a Comment