Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > BindingSource not pushing DataGridView changes.
 

BindingSource not pushing DataGridView changes.

Hi all:

I have a C#.Net 2.0 desktop project and I seem to suddenly be having issues with a DataGridView not updating.

I have a DataGridView (dgv1) bound to a BindingSource (bs1). The bs1 has a DataSet (ds1) as its DataSource and a table (tbl1) inside ds1 as its DataMember. The dgv1 has adding, editing and deleting enabled.

In the dgv1_CellValidating event I validate the user input of a cell. If it passes I call bs1.EndEdit() to push the changes to the DataSet ds1. But I test ds1.HasChanges() immediately aftyer bs1.EndEdit() and no changes are reflected. I was sure this worked previously as this application had been tested. I do not recall making any changes between the last test and now.

Does anyone know why calling EndEdit() on a BindingSource might not be pushing changes?

Thanks.

Kurt

Kurt Wimberger  Tuesday, September 08, 2009 4:44 PM

Futher info:

It seems that the DataGridView is skipping data, or rather saving the data changed in the cell previous to the cell that is actually calling the event. Let me explain:

I go into the database and manually enter a value in the first column, just to have something there to test. I open my form and the DataGridView shows that piece of data.

1) I click in the second column/cell and enter a date.

2) The cell_Validating event fires.

3) The date passes my validation.

4) I test the dataset and find it has no changes, as expected.

5) I call .EndEdit() on the BindingSource tied to that DataGridView.

6) I test the dataset and find it has no changes, NOT as expected.

7) The grid cell shows the date I just entered, but no data has been pushed to DB.

8) Next DataGridView cell is active. I click inside and enter a date.

9) Cell_Validating is called.

10) Value passes my validation.

11) I test the dataset and find it HAS changes, NOT as expected because I have not yet called .EndEdit().

12) I call .EndEdit() on the BindingSource tied to that DataGridView.

13) Changes are pushed to DataSet.

14) DataSet updates database, but ONLY with the first date I entered in the first cell edited, not the current one.

15) Second data value entered disappears from grid (I assume because of a refresh as this date isn’t in the database.)

Ok, so what am I doing wrong? I guess I have the wrong event chosen in which to try and save data/push changes, but I can’t figure out which event is better. Why is there a “delay�in the BindingSource pushing changes back to the dataset?

Kurt Wimberger  Tuesday, September 08, 2009 6:00 PM

No one?

I have solved the issue by placing a manual call to CommitEdit of the DataGridView inside the Cell_Validating event on successful validation.
((

DataGridView)sender).CommitEdit(DataGridViewDataErrorContexts.Commit);

That works but it has the stench of an idiot fix to me. I can't help but believe this is just the wrong event for this. Where do YOU all place calls that update data/push changes from the control to the BindingSource?

Thanks.

Kurt Wimberger  Tuesday, September 08, 2009 7:50 PM
Hi Kurt Wimberger,

After I read the post, I feel it is similar to some previous thread I have handled. Here is my explian.

Do you click a cell of another row to fire the CellValidating event? If you click another cell of the current editing row . The changed data won't be commit to the datasource even if you call EndEdit method to force committing. But if you click a cell of another row to fire the CellValidating event, the changed data will be written to the datasource immediately even if you don't call EndEdit. This is a by design feature of DataGridView control.

You can test it again with my suggestion, if it is not what I mentioned, please tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, September 10, 2009 6:40 AM
We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post editor window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
Kira Qian  Tuesday, September 15, 2009 8:11 AM

You can use google to search for other answers

Custom Search

More Threads

• need help in RowFilter of windows forms datagrid
• in this function i want to get all buttons
• adding a new row in grid
• How to save the single quoted data to the databse? VB.net or VS 2005(vb)
• Changing the databasource of a DataGridViewComboBoxCell at runtime
• Windows application to SQL server Connection
• Combobox in DataGridView Cell problem
• receive of class udpclient in vs 2003...
• DataGridView: programatically add new row for bound grid?
• Help...DatagridColumn Problem