Saturday, 10 September 2016

winforms - Manually adding a new row to DataGridView does not immediately update the bound DataTable

I have a c# windows form app written in .net-4.0



I have a datagridview (dgvItems) which I programmatically bind to a dataset and datatable at the end of an import function.




I have an import button that imports data from a selected excel file into a table called _dtItemAdjustList, once the data has been imported I bind that table to my grid. The code I use to bind the grid is:



dgvItems.DataSource = _dsQtyAdjust;
dgvItems.DataMember = "Item Adjust List";
dgvItems.Refresh();


Everything works fine so far, if I edit the imported data in the datagridview, it updates the bound table _dtItemAdjustList after each cell is edited.




My issue comes in when I try to manually add a row to my datagridview, it doesn't immediately add that new row to the bound datatable.



Example: I put a break point at the end of my dgvItems_CellValueChanged event and added a _dtItemAdjustList.Rows.Count watch and here is what happens.



After the data is imported and I edit one of the existing imported lines the watch shows the correct row count, lets say 5.



Now I click the last * row, and type something into my item# column and hit tab, the break point fires but my row count still only shows 5, I fill out a few more cells in the new row but each time I leave a cell and my CellValueChanged event fires and the row count remains at 5.



Next I add a second manual row, now immediately after I tab out of the first cell I filled out for this second new row, my row counter goes to 6, but technically at this point I've added 2 manual rows to my imported 5, so the counter should read 7




This repeats, basically each new manually added row to the datagridview isn't added to the bound datatable until either a) another row is added or b) I go back and re-edit a cell on an existing row.



Edit
Forgot to mention I tried binding my DataGridView two ways:



DataSource = _dsQtyAdjust //dataset Name
DataMember = "Item Adjust List" // Table Name in the dataset

DataSource = _dtItemAdjustList



It made no difference as far as my new row behaviour goes.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...