selected row into array

Marvin_G

Member
Joined
Jul 30, 2013
Messages
7
Programming Experience
1-3
Hello guys, i have a code that would delete a row from datagridview and mysql. Now what i wanted to do is to delete multiple row instead of one. possible?

VB.NET:
 command.CommandText = "delete from " & cmbTable.Text & " where ID = " & Val(DataGridView1.CurrentRow.Cells(0).Value.ToString) 


                ' TextBox1.Text = DataGridView1.SelectedRows(0).Cells(0).ToString()


                command.CommandType = CommandType.Text
                command.Connection = conn
                command.ExecuteNonQuery()
 
There's nothing in that code that is going to delete anything from a DataGridView. What you should be doing is:

1. Use a data adapter to populate a DataTable by calling Fill.
2. Bind the DataTable to a BindingSource.
3. Bind the BindingSource to the DataGridView.
4. To delete the selected row in code, call RemoveCurrent on the BindingSource. This has no effect on the database.
5. Once you've deleted all the rows you want locally, call Update on the same data adapter to save the changes back to the database. This works for new, modified and deleted rows.
 
I tried my code and the data is deleted in the datagrid and inside database as well. it just a simple code though. why u need to delete it locally and then only update changes to database? it uses a lot of memory if i have a large code. what i want is to directly delete from database. not locally if possible.
 
Sorry, you're not making a lot of sense there.

Firstly, I can guarantee you 100% that the code you posted does not delete anything from a DataGridView. Maybe you have some other code that updates the grid but the code you posted does not.

Secondly, you obviously have a local copy of your data so on what basis are you judging that it would take a lot of memory to delete locally first and then save the changes but it would not to delete in the database first and then update the local copy?

If you want to disregard my 10 years of experience using ADO.NET then that is obviously your prerogative but I'm telling you that what you are trying to do is WRONG. The proper way to do this (and the way ADO.NET has been designed to do it) is to retrieve the data, display it, make all the required changes to that local copy and then save those changes in a batch. That is how I do it and that is how thousands of developers around the world do it. If you think that you know better then it is absolutely your right to do it the wrong way.

With that in mind, if you want to delete multiple records using a single DELETE statement then you need to modify the WHERE clause appropriately. At the moment you have it specifying a single ID so it will match a single record. If you want it to match multiple records then you have to specify multiple IDs. You could do that by using a single condition with an IN operator or you could use multiple conditions separated by OR operators. It would be a mistake to do that but the choice is yours.
 
Back
Top