Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project written in Visual Studio 2008 (VB NET) using a data bound Datagridview control and BindingNavigator (w/ menustrip) control. If I select the Add New button on the Binding Navigator the Datagridview control moves to a new record with an index of -1 but does not add a record to the database.

When I add a record to the database via a query Me.TableAdapter.AddRow() the row is added to the database and the Datagridview control moves to a new record with an index of -1 but does not refresh with the new row.

Using the NewRow of the Datagridview produces worse results, so I have disabled allowing users to add new rows to the Datagridview.

With the following code

VB
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click

   Me.TableAdapter.AddData() 'inserts a new row
   BindingSource.ResetBindings(True)

end sub


it works (adds a row to the database (SQL) and updates the datagridview which I can then modify. However, I still end up with a negative index for that row. Adding additional rows decreases my index (-2, -3, etc). Any ideas anyone?
Posted
Updated 11-Jun-14 5:36am
v2

 
Share this answer
 
Comments
Member 10878403 11-Jun-14 13:18pm    
Still does not solve the indexing issue.
Then you have to debug and find where exactly it is getting index -1.
Solved - I found another thread with a similar problem. When you add the bindingNavigator Visual Studio will add some code on its own. Normally, when a user clicks the bindingNavigatorAddItem button this code is fired in addition to any code you add giving you an out-of-bounds record index. In the InitializeComponent procedure I located and disabled the line

Me.BindingNavigator1.AddNewItem = Me.BindingNavigatorAddNewItem

Doing so resolved my problem.
 
Share this answer
 
Solved - I found another thread with a similar problem. When you add the bindingNavigator Visual Studio will add some code on its own. Normally, when a user clicks the bindingNavigatorAddItem button this code is fired in addition to any code you add giving you an out-of-bounds record index. In the InitializeComponent procedure I located and disabled the line

Me.BindingNavigator1.AddNewItem = Me.BindingNavigatorAddNewItem

Doing so resolved my problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900