c# - Add Rows In a empty dataGrid -
i building mvvm - wpf application. have few datagrids crud operations work fine.
now, want datagrid empty @ beginning , of course can add rows. can fill when click save, nothing saved.
why?
viewmodel:
public class invoiceviewmodel : viewmodelbase { public context ctx = new context(); public invoiceviewmodel() { this.collection = new observablecollection<invoice>(); } private observablecollection<invoice> collection; public observablecollection<invoice> collection { { return collection; } set { collection = value; onpropertychanged("collection"); } } private invoice _selected; public invoice selected { { return _selected; } set { _selected = value; onpropertychanged("selected"); } } private void get() { ctx.invoices.tolist().foreach(invoice => ctx.invoices.local.add(invoice));; collection = ctx.invoices.local; } private void save() { foreach (invoice item in collection) { if (ctx.entry(item).state == system.data.entity.entitystate.added) { ctx.invoices.add(item); } } ctx.savechanges(); } private void delete() { var id = selected; var invoice = (from in ctx.invoices i.idinvoice == id.idinvoice select i).singleordefault(); collection.remove(invoice); } #region "command" // private icommand getcommand; private icommand savecommand; private icommand removecommand; /*public icommand getcommand { { return getcommand ?? (getcommand = new relaycommand(p => this.get(), p => this.canget())); } } private bool canget() { return true; }*/ public icommand savecommand { { return savecommand ?? (savecommand = new relaycommand(p => this.save(), p => this.cansave())); } } private bool cansave() { return true; } public icommand deletecommand { { return removecommand ?? (removecommand = new relaycommand(p => this.delete(), p => this.candelete())); } } public bool candelete() { if (selected != null) return true; else return false; } #endregion }
view:
<page.resources> <local:invoiceviewmodel x:key="invoice" /> <local:shopviewmodel x:key="shop" /> <local:supplierviewmodel x:key="supplier" /> <local:productviewmodel x:key="product" /> <datatemplate x:key="productdatatemplate"> <textblock text="{binding product}" /> </datatemplate> </page.resources> <datagrid x:name="datagridinvoice" margin="5" grid.row="1" itemssource="{binding collection}" autogeneratecolumns="false" selecteditem="{binding selected, mode=twoway}" selectionmode="extended" selectionunit="fullrow"> <datagrid.columns> <datagridtextcolumn x:name="datagridtextcolumn" header="supplier invoice nb" width="*" /> <datagridcomboboxcolumn header="ref supplier" itemssource="{binding products, source={staticresource supplier}, mode=oneway}" displaymemberpath="refsup" selectedvaluebinding="{binding refsupp}" selectedvaluepath="refsup" width="*" /> <datagridtextcolumn header="unit" binding="{binding unit, updatesourcetrigger=propertychanged, mode=twoway}" width="*" /> <datagridtextcolumn header="quantity" binding="{binding quantity, updatesourcetrigger=propertychanged, mode=twoway}" width="*" /> <datagridtextcolumn header="prix/moq" binding="{binding unitprice, updatesourcetrigger=propertychanged, mode=twoway}" width="*" /> <datagridtextcolumn header="total price" binding="{binding totalprice, updatesourcetrigger=propertychanged, mode=twoway}" width="*" /> </datagrid.columns> </datagrid> <stackpanel orientation="horizontal"> <button x:name="btndelete" content="delete" command="{binding deletecommand}" horizontalalignment="center" margin="100,5,5,5" width="85" /> <button x:name="btnadd" content="save" command="{binding savecommand}" horizontalalignment="center" margin="20,5,5,5" width="85" /> </stackpanel>
you have method binds collection
cached local of entity set. without binding, it's harder save added items:
private void get() { ctx.invoices.tolist().foreach(invoice => ctx.invoices.local.add(invoice));; collection = ctx.invoices.local; }
in fact can load invoices , cache local:
private void get() { ctx.invoices.load(); collection = ctx.invoices.local; }
however said want empty data grid @ first (just adding), can refactor get
method accept bool indicating if data should loaded db first or not:
private void get(bool loaddatafirst) { if(loaddatafirst) ctx.invoices.load(); collection = ctx.invoices.local; }
now using get
method without loading data first, collection
still bound local
cache (which should empty). after adding new items collection
, local
have items added automatically , save
can call savechanges
:
private void save() { ctx.savechanges(); }
the get
method should used inside constructor replacing this.collection = new observablecollection<invoice>();
this:
public invoiceviewmodel() { get(false); }
Comments
Post a Comment