I have a datagridview thats bounded to a db and bindingNavigator that has Add/Delete/Save buttons. When I select a row and delete it, the row gets deleted from the datagridview, but when I restart the application, it doesn't get physically deleted from the database. Search suggests putting this code inside the save button and press it before closing the application:

this.Validate();
        this.itemsBindingSource.EndEdit();
        this.itemsTableAdapter.Update(this.myDatabaseDataSet.Items);

        MessageBox.Show("Update successful");

But the database still have the row undeleted.

And using itemTableAdapter.Delete() method isn't really helpful since I've more than 30 columns in the table.

Any help?.

If you are using tableadapters, i would recommend adding your own delete query. The standard ones are pretty heavy going, you have to put the value for every single column.

If you add a new query to the table adapter along the lines of:

DELETE FROM [TableName] WHERE ([PrimaryKeyColumn] = @Key)

You can call it like

itemTableAdapter.DelteQuery('keyvalue');

If you are using tableadapters, i would recommend adding your own delete query. The standard ones are pretty heavy going, you have to put the value for every single column.

If you add a new query to the table adapter along the lines of:

DELETE FROM [TableName] WHERE ([PrimaryKeyColumn] = @Key)

You can call it like

itemTableAdapter.DelteQuery('keyvalue');

Thanks but I get 'Error parsing query text' and .DelQuery(); doesn't accept 1 parameter 'Keyvalue', but I've to put in all 30+ columns values!!

Please take a look at this sample solution. You should notice add/del/save buttons have no affects on the db.

Alright I found a solution, the code should be:

this.Validate();
            this.itemsBindingSource.EndEdit();
            this.itemsTableAdapter.Update(myDatabaseDataSet.Items);
            this.myDatabaseDataSet.AcceptChanges();
            MessageBox.Show("Update successful");

And the db in bin\dir\"dbname" gets updated. Thanks for the help.

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.