Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

How do I delete a row from table and database (Windows Forms .Net)?

Hi,

I'm new to Windows Forms and .Net programming with database access.

I'm trying just a single form with some textboxes on it and a simple table.
There's a table opened with several rows.

In my form I have a bindingSource, a tableAdapter and a DataSet.

After reading here and there, I found that I need this 5 lines in order to delete a row!!
But.... really!?

            DataRowView drv = (DataRowView)bindingSource1.Current;
            MyDataSet.TestRow row = (MyDataSet.TestRow)drv.Row;
            this.testTableAdapter.Delete(row.Col1, row.Col2);
            this.bindingSource1.EndEdit();
            this.bindingSource1.RemoveCurrent();

Is there any simpler approach? I mean, less lines.
0
fischermx
Asked:
fischermx
  • 5
  • 4
1 Solution
 
Nash2334Commented:
You TableAdapter has an update method that will insert, update or delete a record based on it's current rowstate, calling the appropriate stored procedure for you.  That is the big plus with using TableAdapters, you don't have to specify insert, deletes or updates (although you can).

Your BindingSource will bind the DataTable of the DataSet to the UI component, perhaps a DataGridView in your case.  This will act as the "traffic cop" for you, committing changes to the database when the TableAdapter's Update method is called.  In other words, if you have a DataSet/DataTable bound to a BindingSource bound to a DataGridView (as your UI component), all you need to do is call the Update method of your TableAdapter to delete a row after it's been deleted in the UI.

MyTableAdapter ta = new MyTableAdapter();
ta.Update(myDataSet.MyDataTable);
0
 
fischermxAuthor Commented:
Nash:

I don't get it when you say "all you need to do is call the Update method of your TableAdapter to delete a row after it's been deleted in the UI."

I mean, well, that's my question, I want to disappear a record, kill it, of course from the cached data in the UI and in the table.

So, according to your explanation, what are the steps to delete record? (all the steps)

0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Nash2334Commented:
Are you bound to a DataGridView?
0
 
fischermxAuthor Commented:

No, the data is bound to textboxes.

0
 
Nash2334Commented:
OK, so if I am reading this correctly you've got a bunch of text elements on a form, and they are bound to a single record in your database.  It sounds like you are using a BindingNavigator.  You click the BindingNavigator delete button to delete the current record.

If this is correct, the BindingNavigator has it's own delete method wired to the button that will perform all actions for you as long as you have a valid delete command and the primary key of the record is available.  You only need to write all that code if you are manually handling the delete yourself, e.g. in the case where you want to prompt for delete confirmation.

If you are not using a BindingNavigator, and are wired directly to a button or linkbutton click event, some more code would be helpful to see where there are opportunities for efficiencies, including where you are doing your DataBinding.

0
 
fischermxAuthor Commented:
I showed my code in the original post:

 DataRowView drv = (DataRowView)bindingSource1.Current;
MyDataSet.TestRow row = (MyDataSet.TestRow)drv.Row;
this.testTableAdapter.Delete(row.Col1, row.Col2);
this.bindingSource1.EndEdit();
this.bindingSource1.RemoveCurrent();

My questions
Is this the correct way? (Yes/No)
Is this the only way? (Yes/No)
Is there a way with fewer lines? (Yes/No)
0
 
Nash2334Commented:
1. Yes, this is ONE correct way.
2. No
3. Depends, which is why I was asking if you were using a BindingNavigator.  Your code does not tell me how you are binding to your UI controls or loading your data into the BindingSource, hence the request for more information.  If that's all you can provide, then I'd say run with what you have.
0
 
fischermxAuthor Commented:
I don't have a binding navigator.
But if that will help, I can put that, and hide it to the user. (I don't want that ugly thing in my UI).

So, let's suppose I have a binding navigator. How can that simplify things?

0
 
fischermxAuthor Commented:
Thank you!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now