1,105,263 Community Members

How to delete selected row from datagridview physically from the database?

Member Avatar
Yamazaki
Newbie Poster
15 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?.

Member Avatar
Ryshad
Nearly a Posting Virtuoso
1,263 posts since Aug 2009
Reputation Points: 480 [?]
Q&As Helped to Solve: 247 [?]
Skill Endorsements: 14 [?]
 
1
 

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');
Member Avatar
Yamazaki
Newbie Poster
15 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Attachments HeadFirst.zip (164.22KB)
Member Avatar
Yamazaki
Newbie Poster
15 posts since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Question Answered as of 4 Years Ago by Ryshad
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: