I am trying to make a simple demo of a DataGridView bound to a database table. I want to add a row, delete a row, and save the table.

I used the IDE to do just about everything. It created the BindingSource when I set the datasource of the DataGridView to a table of a DataSource I added to the project.

Now the only code looks like this:

Public Class Form1

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'TestDataSet._Event' table. You can move, or remove it, as needed.
        Me.EventTableAdapter.Fill(Me.TestDataSet._Event)
        cmbReason.SelectedIndex = 0
    End Sub

    Private Sub btnDeleteRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteRow.Click

        DirectCast(Me.EventBindingSource.Current, DataRowView).Delete()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Me.EventTableAdapter.Update(Me.TestDataSet)

    End Sub

    Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
        Dim NewEvent As DataRowView = DirectCast(Me.EventBindingSource.AddNew, DataRowView)

        'add all the attributes to the new row
        NewEvent.Item("associate") = "Test"
        NewEvent.Item("inout") = "IN"
        NewEvent.Item("reason") = cmbReason.SelectedItem

        NewEvent.Item("timefield") = Now
        NewEvent.EndEdit() 'commit
    End Sub
End Class

I set the AutoIncrementStep to 1 in my Key field in the dataset (the .xsd). I left the Key field visible in the DataGridView and it seems to be adding the correct Key when a row is added. However, I still occasionally get the "concurrency violation" error if I just mess around with these 3 buttons. Any idea why/how to prevent that?

Thanks,

Dave

Please visit the following link for more information:
http://www.vbforums.com/showthread.php?t=544701

If you do not mind, please let me know if this helped you or not.

I have been struggling with a very similar issue and have searched all over for an answer.

Here's the setup in a nutshell:

I'm using VB express 2010 with an Access(2007) database. So far I have not written any code, but have been using all the built in wizards to do everything.
1. I built a simple 1 table database in access
2. I added the database.accdb using "Add New Data Source"
3. I drug the table from the data source onto my form, creating a DataGrid.
4. I also drug a couple of fields onto my form, creating some input boxes.
5. Across the bottom on the Development screen I have:
XXXDataSet
YYYBindingSource
YYYTableAdapter
TableAdapterManager
XXXBindingNavigator

XXX seems to be the name of my database
YYY seems to be the name of the table in the database

6. I run the program
7. I am only entering data in the text boxes, but I move through the dataset with both the Binding Navigator and the Datagrid.
8. I can press the yellow cross and add a new record to the dataset
9. I press the disk symbol and most of the time it writes the dataset to the Access Database
10. BUT.. sometimes, and I can't figure out an exact repro yet, I get a "Concurrency Violation"
11. It only takes a few minutes to get the problem occur

From what I read I need to do an "EndEdit()" on the Binding Source before writing the data to the Database, but that code is already in the BindingNavigatorSaveItem_Click event (code generated when I installed the BindingNavigator control).

So I'm kind of stumped on what to do.

BTW I did put a Try/Catch around the update, which was suggested by one website, and all I got was "-1 Concurrency Violation".

Since I'm not writing any of the code myself, and introducing bugs, I figured it should just work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.