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