c# - Updating DataSource of DataGridView - old rows not removed -
i have application datagridview. use sqlite database , set table datasource datagridview: i'm getting dataset using sqlitedataadapter , setting datasource:
grid.autogeneratecolumns = false; grid.datasource = ds.tables[0].defaultview; grid.columns["criteriaid"].datapropertyname = "rowid"; grid.columns["criterianame"].datapropertyname = "name"; grid.columns["criteriadesc"].datapropertyname = "description"; grid.columns["criteriadirection"].datapropertyname = "direction";
so far seems working want. want update datasource, replacing rows other data dataset got source. wrote:
var originalds = grid.datasource dataview; var originaltable = originalds.table; originaltable.rows.clear(); foreach (datarow row in ds.tables[0].rows) { var newrow = originaltable.rows.add(); foreach (datacolumn column in ds.tables[0].columns) { object item = row[column]; var columnname = column.columnname; if (originaltable.columns[columnname] != null) { newrow[columnname] = item; } } }
still, works expected, rows in datagridview replaced new data. but. trying update underlying database these changes, calling:
var cmdb = new sqlitecommandbuilder(adapter); adapter.updatecommand = cmdb.getupdatecommand(); var cm = (currencymanager)grid.bindingcontext[grid.datasource, grid.datamember]; cm.endcurrentedit(); grid.endedit(); adapter.update(ds.tables[0]);
where adapter sqlitedataadapter used read data db first. , here i'm stuck problem: instead of deleting previous data , saving new rows database, previous records still in database , new rows new datasource added. it's adding , not replacing rows. in line above adapter.update(ds.tables[0]);
when debug can see ds.tables[0] has rows new source. why aren't old rows deleted? can make work?
deletng rows db works when delete rows in datagridview manually using gui , save same way database.
any ideas?
Comments
Post a Comment