c# - Update database linked to DataGrid dynamically -
i want put several buttons on wpf form , update dynamically datagrid data different database each button, let say, have 3 databases , 3 buttons, , when push 1 button data 1 different database.
but want data on datagrid update database when change made on grid, don't have program "save" stuff.
right now, code have this:
try { oledbconnection conn = new oledbconnection( "provider=microsoft.jet.oledb.4.0; " + "data source=materialdb.mdb"); oledbdataadapter adapter = new oledbdataadapter("select * mytable", conn); mydataset = new dataset(); adapter.fill(mydataset, "mytable"); var = catalogsgrid.getbindingexpression(datagrid.itemssourceproperty); catalogsgrid.datacontext = mydataset; catalogsgrid.itemssource = mydataset.tables[0].defaultview; binding nbe = new binding(); nbe.source = mydataset; nbe.mode = bindingmode.twoway; nbe.updatesourcetrigger = updatesourcetrigger.propertychanged; nbe.path = new propertypath("mytable"); nbe.bindsdirectlytosource = true; catalogsgrid.setbinding(datagrid.itemssourceproperty, nbe); catalogsgrid.items.refresh(); }
this code loads data database grid, edit data , doesn't update ms access table.
oh, still not coded "mytable" should string put name of different tables want show , update grid.
what missing?
the datagrid not save changes automatically, that's (your application) decide, writes changes underlying datatable.
so understand, want avoid having save button , save changes database after have changed. need know when has changed, , dataset can that:
mydataset = new dataset(); mydatatable = mydataset.tables["mytable"]; adapter.fill(mydatatable); // update db whenever row changes mydatatable.onrowchanged += (s, e) => { adapter.update(mydatatable); }
Comments
Post a Comment