Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Selecting new row in datagridview adds datasource record?
 

Selecting new row in datagridview adds datasource record?

Hello,
This is a strange problem but I hope the solution is something obvious to someone else!
I have a datagridview that has AllowUserToAddRows set to True, I also have a table in my dataset bound to the datagridview using a bindingsource. There is a bindingsource navigator attached to the datagridview that can be used to navigate the rows.

Here are the steps to replicate my problem (in my application only, have tested in another application and it doesn't happen)

- Enter something into a cell.
- Tab off the cell.
- A "new row" is then shown on the datagridview below the row that I am working in.

When I have a cell selected on the first row, the bindingnavigator shows 1 of 1 rows. This is fine.

- Click on the "new row".

The bindingnavigator now reads 2 of 2. This is also fine.

- Click back to the first row.

The binding navigator again reads 1 of 1. No worries. But now is when it gets weird...

- Double click on a cell in the first row to go into edit mode for that cell.
- Do not tab off the cell when you have finished editing, but instead either press down or click directly onto the next row .

The bindingnavigator reads 2 of 2. Okay.

- Click back on the first row.

Bindingnavigator now reads 1 of 2. Nooo!

This is when the problems start.
You can keep doing this as many times as you like and the number of rows will continue to increment. Eventually when you go up and down without going into edit mode on either row, the system will throw an OutOfRangeException as you would expect.

It seems that when going back up to the first row, the rowLeave event is triggered, then my underlying datasource.count increments by one and then dataBindingComplete fires. When stepping through the code there is nothing in between those two events. What makes the datagridview decide to add a row to the datasource in general? Has my code made some changes to this new row somewhere along the line that has tricked the dgv into thinking that it is no longer a "new row"?

Any ideas or advice would be greatly appreciated. Thanks in advance :)
nicgordon  Friday, July 03, 2009 8:36 AM
Thanks for your suggestions! I checked all of the values in the binding source when the first cell was not in edit mode and then compared it to all values when the cell was in edit mode. Unfortunately they were exactly the same.
I still don't really know what is causing this to happen, however I know that the count only increments when I select the next row while in edit mode on a cell in the current row. So my solution was on DataGridView_CellLeave to do a bit of:

    Private Sub DataGridView_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellLeave
        If Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).IsInEditMode Then
            Me.DataGridView.EndEdit()
        End If
    End Sub
And this seems to have stopped it from happening. If anyone is able to shed any light on this I'd still love to hear it, but for now, this will do.
  • Marked As Answer bynicgordon Monday, July 13, 2009 12:20 AM
  •  
nicgordon  Monday, July 13, 2009 12:20 AM
Hmm, I tried your scenario but could not repro.
I setup the datagrid view with the following simple steps:
1) Create a winform project
2) Create a dataset using the dataset designer.
3) Show the datasource window, see the datatable there and drag drop the datatable to the form

This will setup a datagridview binding to a bindingsource, which set the datasorce to the datatable.
4) F5 run the app
5) Double click on a cell in the last row, type something
6) press down key or click on the new row. The navigator shows total row count = (n+1)
7) click back to the row in step 5. The navigator shows total row count = (n)


Did you use the Visual Studio designer to setup your app as i have show or did it your own?
Did you add any events of your own to the binding source, dataset or datagridview?
John Chen MS  Saturday, July 04, 2009 5:33 PM
There is lots of code on this form, I didn't write it originally so yes, there could be any number of methods that could be causing this to happen.
I am just wondering whether anyone can have a blind stab at where the issue might be originating.
I suppose my question is, what code can cause a "new row" to become counted as an added actual row?
nicgordon  Monday, July 06, 2009 2:40 AM
My understanding is the DGV will cancel the add new mode is there is no actual editing in the new row. Maybe you can debug in your scenario what is the values in the binding source items.
John Chen MS  Monday, July 06, 2009 7:14 AM
Thanks for your suggestions! I checked all of the values in the binding source when the first cell was not in edit mode and then compared it to all values when the cell was in edit mode. Unfortunately they were exactly the same.
I still don't really know what is causing this to happen, however I know that the count only increments when I select the next row while in edit mode on a cell in the current row. So my solution was on DataGridView_CellLeave to do a bit of:

    Private Sub DataGridView_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellLeave
        If Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).IsInEditMode Then
            Me.DataGridView.EndEdit()
        End If
    End Sub
And this seems to have stopped it from happening. If anyone is able to shed any light on this I'd still love to hear it, but for now, this will do.
  • Marked As Answer bynicgordon Monday, July 13, 2009 12:20 AM
  •  
nicgordon  Monday, July 13, 2009 12:20 AM

You can use google to search for other answers

Custom Search

More Threads

• Cannot see my new records in my data grid control
• Best way to do sorting in a listbox that is bound to a dataset???
• DataBindingComplete event firing incorrectly??
• DataGridView CurrentCell jumps to wrong row
• Multiple tables in data relations
• Limits on the number of tables or fields in a datasource?
• .NET 2005: Bind Windows.Forms.Label.Text property to an integer member in the parent Windows.Forms object
• how to represent 2 D data in Grid View?
• Downloading of form control from HTTPS
• Hierarchical datagrid from one datatable