0

I have a DataGrid bind to a DataSet. On change or adding rows I'm trying to save the changes. I add the delegate for a RowChange event of the dataset which is calling itself after dataAdapter.Update(row) many times!!

NewGeoDataSet.RegionGeoDataTable reg = new NewGeoDataSet.RegionGeoDataTable();
regAdapter = new NewGeoDataSetTableAdapters.RegionGeoTableAdapter();
reg = regAdapter.GetData();
datagrid.DataContext = reg;
datagrid.ItemsSource = reg;
reg.RowChanged += new DataRowChangeEventHandler(dataset_RowChanged);

Delegate

private void dataset_RowChanged(object sender, DataRowChangeEventArgs e)
{
    Console.WriteLine("row changed "+e.Action);
    DataRow r = e.Row;      
    regAdapter.Update(r);
    Console.WriteLine("data updated");

}

And eventually I'm getting:
row changed Change
row changed Change
Exception like: there is already an open datareader associated with this command
row changed Change
Exception again
row changed Commit
data updated
data updated

So the question is: why dataAdapter.Update(row) firing itself. How can I overcome it? I've tried to use bool variable to prevent further executions, but it's not reliable and dirty.

I've checked http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples it seems the same but doesn't work...

What possibly am I doing wrong?

  • As a just quick glance, datatext and itemsource together seems weird..but not sure..Maybe, only itemsource might be enough? – Kay Lee May 19 '16 at 14:15
  • well, It works ok with only itemSource binding (probably I did it just for sure). But still works the same way... When it reaches `Update` function it goes to `RowChanged` again kinda recursive.. 3 times. – Marie Devjatykh May 20 '16 at 07:29
  • I've checked sample above (by the link) and it fires also many times. Very strange and stupid. So, I suppose it's a common behavior. I added condition before update `e.Action != DataRowAction.Commit` to prevent multiply commits. And amount of exceptions and duplicate calls will depend from amount of columns... – Marie Devjatykh May 20 '16 at 12:59
  • It seemed you already have enough knowledgements to manage your problem and this idea made me thought you will solve soon by yourself and lost timing. Have you solved now? or still? There seems many simple ways and I doubted you have to use only this way. Just to welcome. Have good day. – Kay Lee May 22 '16 at 08:41
  • I haven't solved it the way I want - it still firing itself but it saves changes to the database though. I just don't like the way how it works - presence of exceptions is not very good, right? What are the other ways to do it? – Marie Devjatykh May 23 '16 at 06:47
  • Your link seems to need WPF Xceed Tool Kit nuget package Library. Have you installed it..? And where is you want to update the changes to ? I can only support SQL database..However, it seems simple process getting the changed values and update to someplace. – Kay Lee May 23 '16 at 07:05
  • Intentionally, I wrote(re-produced) this code as an answer for someone and it's working fine to update changes of DataGrid rows to SQL. If you're ok, just shortly look into this code. http://stackoverflow.com/questions/37116407/updating-datagrid-row-to-save-to-sql/37141454#37141454 – Kay Lee May 23 '16 at 07:31

0 Answers0