Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridview with a TableAdapter Update Also Inserting a New Record
 

Datagridview with a TableAdapter Update Also Inserting a New Record

Hello Everyone:

I am using a datagridview with a binding source with a toolstripbutton that when I click the “save�button, it calls out to my code that runs this code:

dgvUsers.CurrentCell = dgvUsers.Rows[dgvUsers.Rows.Count - 1].Cells["UserID"];

tUsersBindingSource.EndEdit();

tUsersTableAdapter.Update(usersDataSet.tUsers);

What I can’t for the life of me figure out is that after it calls the update, another row is added to the datagridview, hence another row has been added to the database, which is blank. All I did was click on the update button which is supposed to call out to the dataadapter.update command and simply update the records, not add a new record as well.

I can’t find where an insert is being done as well in the code. Does anyone know why this is happening or how to stop it? I don’t want a new record just by calling an update.

armst1111  Tuesday, September 29, 2009 8:26 PM

Hi armst1111,

From my experience, the root cause is the code snippet below:
dgvUsers.CurrentCell = dgvUsers.Rows[dgvUsers.Rows.Count - 1].Cells["UserID"];
This would cause the new row become the current row and the underlying binding source add a new record.

To solve this issue, there are three ways:

1. Modify that line of code to set another row to be the current row, not the new row.

2. Remove that line of code.

3. Set the AllowUserToAddRows property of the DataGridView to false to disallow the new row to be inserted automatically. If there is an binding navigator, the user can use the add button to add a new row. If there is not, you need to add a button and call the AddNew method of the BindingSource to add a new record in its click event handler.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 01, 2009 8:42 AM

Hi armst1111,

From my experience, the root cause is the code snippet below:
dgvUsers.CurrentCell = dgvUsers.Rows[dgvUsers.Rows.Count - 1].Cells["UserID"];
This would cause the new row become the current row and the underlying binding source add a new record.

To solve this issue, there are three ways:

1. Modify that line of code to set another row to be the current row, not the new row.

2. Remove that line of code.

3. Set the AllowUserToAddRows property of the DataGridView to false to disallow the new row to be inserted automatically. If there is an binding navigator, the user can use the add button to add a new row. If there is not, you need to add a button and call the AddNew method of the BindingSource to add a new record in its click event handler.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 01, 2009 8:42 AM

You can use google to search for other answers

Custom Search

More Threads

• How Do I Sort A DataTable Using Related DataTable Values?
• how can i seprate the text
• SqlConnection and sp_reset_connection problem
• how to do restrict char in datagridviewcell?
• Prevent edit DataGridViewCheckBoxCell
• Displaying Names rather than IDs in a ComboBox
• Problem refreshing DataTable
• Data Reader Data to an Array???
• How to print my DataGridView
• What Mapping Name do I use when DataGrid.DataSource is IList<InterfaceItem>?