detecting delete on row in datagridview and updating dataset

ninjaimp

Well-known member
Joined
Jun 13, 2007
Messages
80
Programming Experience
1-3
i have a datagrid which show values in a dataset.

I can add new values fine with the below code, but when i delete one of the rows (highlighting a row and using the delete key) i get the error:

'Update requires a valid DeleteCommand when passed DataRow collection with deleted rows.'

this is flaggin on the bolded line below

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim drNew As System.Data.DataRow

        drNew = Me.ISurfDataSet.Urls.NewRow
        drNew.Item("URLLink") = urlTextBox.Text
        Me.ISurfDataSet.Urls.Rows.Add(drNew)

        Changes = True 'set the flag to show that changes have been made

    End Sub

    Sub AddData() ' add the new urls to the database



        Me.Validate()
        Me.UrlsBindingSource.EndEdit()
        Me.UrlsTableAdapter.Update(Me.ISurfDataSet.Urls)

        MsgBox("All new data added successfully!")

        Changes = False ' all canges have been saved so the form can close

        Me.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call AddData() ' call the function to add the new urls to the database

    End Sub

    Private Sub UrlsDataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles UrlsDataGridView.UserDeletedRow
        Me.Validate()
        Me.UrlsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ISurfDataSet)
    End Sub    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim drNew As System.Data.DataRow

        drNew = Me.ISurfDataSet.Urls.NewRow
        drNew.Item("URLLink") = urlTextBox.Text
        Me.ISurfDataSet.Urls.Rows.Add(drNew)

        Changes = True 'set the flag to show that changes have been made

    End Sub

    Sub AddData() ' add the new urls to the database



        Me.Validate()
        Me.UrlsBindingSource.EndEdit()
        Me.UrlsTableAdapter.Update(Me.ISurfDataSet.Urls)

        MsgBox("All new data added successfully!")

        Changes = False ' all canges have been saved so the form can close

        Me.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call AddData() ' call the function to add the new urls to the database

    End Sub

    Private Sub UrlsDataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles UrlsDataGridView.UserDeletedRow
        Me.Validate()
        Me.UrlsBindingSource.EndEdit()
        [B]Me.TableAdapterManager.UpdateAll(Me.ISurfDataSet)[/B]
    End Sub
 
When you create a Data Source and generate a typed DataSet the wizard uses the schema of your database to generate the schema of the typed DataSet and the methods of the TableAdapters.

In your case, the wizard has been able to generate SELECT and INSERT statements for your table because it simply needs to read the columns to do that. It has been unable to generate UPDATE and DELETE statements though, because to do that it needs to be able to uniquely identify each record. When you delete a record the TableAdapter must be able to tell the database exactly which record to delete, which it does using the primary key value of that record. Your database table must not have a primary key, so no UPDATE or DELETE statement could be generated.

Fix your database by defining a primary key on all your tables. There are some legitimate instances but it's a very rare thing that it's appropriate for a table to not have a primary key. Once you've done that you can reconfigure your Data Source and all will be hunky dory.
 
nice one - for some reason i hadn't set a primery key, but after doing that works perfectly

many thanks for your help
 
Last edited:

Latest posts

Back
Top