We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,868 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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

2
Contributors
3
Replies
10 Hours
Discussion Span
3 Years Ago
Last Updated
6
Views
Question
Answered
Yamazaki
Newbie Poster
15 posts since May 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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');
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 247
Skill Endorsements: 10

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)
Yamazaki
Newbie Poster
15 posts since May 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Yamazaki
Newbie Poster
15 posts since May 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Years Ago by Ryshad

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1225 seconds using 2.64MB