Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > New row not committed in DataGridView
 

New row not committed in DataGridView

Hi.

I have a DataGridView which is bound to a DataTable:
myDataGridView.DataSource = myDataTable;

The user does the following:
  • He creates new row by clicking on the last row of the DataGridView ("*").
  • He fills in the cells of this new row.
  • He navigates to other rows with the mouse or the arrow-keys.
  • He saves myDataTable. Result: The new line is lost.
If he does the same and clicks on one RowHeader (it doesn't matter, which) before saving, then the new row is saved!

I tried to call myDataGridView.EndEdit() before saving, but without any effect.

So how can I make the DataGridView commit the value to the DataTable immediately after it has been created? And why this is not happing automatically?


Wuschba  Wednesday, May 30, 2007 10:30 AM
If there is an data error when the user tries to move to another row the new row will be lost. An example isuser creates a new row and does not fill in a value for a column that does not allow nulls. Ways to prevent this is to assign values to columns that do not allow nulls in the datagridview's defaultvaluesneeded event. Another way is to handle the DataError event.
Ken Tucker  Wednesday, May 30, 2007 10:44 AM
Thanks for your reply. I tried the 2 things you mentioned:
- Adding the DataError-Event -> Is not called at all
- Filling out all fields with correct values: The row is still not committed.

As I said, the row IS committed when clicking on a RowHeader which makes me think an error of data cannot be the reason why the row is not committed.
Wuschba  Wednesday, May 30, 2007 10:57 AM
Okay, I managed it that the DataTable get the new row by calling myDataGridView.EndEdit in CurrentCellChanged. But this only works if the users leaves the new row and jumpd to another row. When the user adds a new row and stays there and then hits my "Save"-Button, the new row is not saved. Yes, I call EndEdit before saving. How can I force the control to commit the new row before saving?

Or is it a differenze working with a BindingSource?

Wuschba  Wednesday, May 30, 2007 12:35 PM

Hi Wuschba,

Try to add the following lines of code in your saving method before you call the EndEdit. This will commit changes in the current cell to the data cache without ending edit mode

Code Snippet

(dataGridView1.IsCurrentCellDirty)

DataGridViewDataErrorContexts.Commit);

I have the same problem in reverse....

I have a datagridview, not connected to a database (I use soap to fill it myself with SQL)

How do I end edit mode in code? I do not care if change gets committed or not since I already stored the change in DB. I just need to end editmode.

Uncle X  Tuesday, September 11, 2007 10:54 AM
Ken Tucker  Tuesday, September 11, 2007 11:10 AM
Nope this does not work. I see the last row on the screen after I save the data, but the last row is not making it to the data table unless I move away from the corresponding grid row. I can't find a CommitRow, but it seems it would be useful.

Michael K2  Monday, December 01, 2008 9:29 PM
This works. dgvProvChoices is the DataGridView. The currency manager supplies what is needed:

if (dgvProvChoices.IsCurrentCellDirty || dgvProvChoices.IsCurrentRowDirty)

{

dgvProvChoices.CurrentRow.DataGridView.EndEdit();

dgvProvChoices.EndEdit();

CurrencyManager cm = (CurrencyManager)dgvProvChoices.BindingContext[dgvProvChoices.DataSource, dgvProvChoices.DataMember];

cm.EndCurrentEdit();

}

Michael K2  Tuesday, August 18, 2009 6:49 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView with BindingSource
• Is it possible to jump to first row with selected letter in the DataGridView?
• Extra Row in Datagridview
• Checkbox in datagrid
• How to update a partial table in to the databse from a dataset
• Datagrid to Datatable
• Master Detail grid with typed dataset.
• Find Values in a DataTable
• insert values into DataBase problem
• Can't Add Rows to Datagridview