Reputation: 1027
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.
Upvotes: 3
Views: 2892
Reputation: 1027
Ok after a lot of trial and error and online searching I found a solution that seems to work.
Instead of binding my DataGridView directly to my DataTable, I created a BindingSource, bound the BindingSource to my DataTable, then bound my DataGridView to the BindingSource and finally in my dgvItems_CellValueChanged
event I used a combination of EndEdit()
and NotifyCurrentCellDirty(true/false)
to make it work.
Code sample:
public partial class frmQuantityAdjustment : Form
{
// In my forms partial class I created a new public BindingSource
public BindingSource bsItemAdjust = new BindingSource();
}
private void frmQuantityAdjustment_Load(object sender, EventArgs e)
{
// In my form_Load event I bound the BindingSource to my DataTable
// and then the DataGridView to the BindingSource
bsItemAdjust.DataSource = _dtItemAdjustList;
dgvItems.DataSource = bsItemAdjust;
dgvItems.Refresh();
}
private void dgvItems_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// Finally at the end of my CellValueChanged event I added the
// following code
bsItemAdjust.EndEdit();
dgvItems.NotifyCurrentCellDirty(true);
dgvItems.EndEdit();
dgvItems.NotifyCurrentCellDirty(false);
}
I came across this solution on this thread and after some testing it seems to work perfectly.
Now the new row in my DataGridView is added to the bound DataTable at the end of the CellValueChanged event.
Upvotes: 5