How to delete multiple selected rows in datagridview?

kfirba

Well-known member
Joined
Dec 29, 2012
Messages
77
Programming Experience
1-3
Hello!

I'm using the OleDB class to represent my database.
what i tried to do is to let the user select multiple rows in the DataGridView and then press "Delete" and it will delete all of the selected rows. It could be 1 row, or even 100 rows.

I couldn't figure out how to do it :(

Thanks in advance!
 
You don't have to do anything. That's the default behaviour. You simply bind your data to the grid, i.e. assign your DataTable to the DataSource property of the grid, and then the user can select multiple grid rows and press Delete to automatically the delete the corresponding records.
 
You don't have to do anything. That's the default behaviour. You simply bind your data to the grid, i.e. assign your DataTable to the DataSource property of the grid, and then the user can select multiple grid rows and press Delete to automatically the delete the corresponding records.

Thanks for your reply!

unfortunately, that is not right for some reason :S
it doesn't delete the selected rows, it's deleting the row that has the ">" sign on the left(which is the last row that you mark).

let's say that it will do that, I still want to know how to do it by myself and not by using the wizard ^^'

I think I got it!

this is what i tried:
VB.NET:
For i As Integer = Me.DollDataGridView.SelectedRows.Count - 1 To 0 Step -1            Me.DollDataGridView.Rows.Remove(DollDataGridView.SelectedRows(i))
        Next

Well, i'm not 100% sure what the SelectedRows is, but i think that it's a MINI table with the selected rows only and when i specify an index, i get the row selected index in the data grid view? for example,
i got this grid view:

NAME AGE
X 12
Y 15
T 10
B 9

and I selected Y and B, the SELECTEDROWS holds now a table that looks like that:

Y 15
B 9

and when i access the index of 1, which is B in the SELECTEDROWS, i get the actual index in the REAL table, which means i get the number 3.

is that correct?
 
Last edited:
unfortunately, that is not right for some reason
Yes it does. If it didn't work for you then you did it wrong.
let's say that it will do that, I still want to know how to do it by myself and not by using the wizard
This has got nothing to do with wizards. You simply bind a DataTable to the grid and it works. Follow these instructions to see for yourself:

1. Create a new Windows Forms Application project.
2. Add a DataGridView to the form and set its Dock property to Fill.
3. Add the following code to the form:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.DataSource = GetData()
End Sub

Private Function GetData() As DataTable
    Dim table As New DataTable

    With table.Columns
        .Add("ID", GetType(Integer))
        .Add("Name", GetType(String))
    End With

    With table.Rows
        .Add(1, "Peter")
        .Add(2, "Paul")
        .Add(3, "Mary")
        .Add(4, "John")
    End With

    Return table
End Function
4.Run the project.
5. Ctrl+Click multiple rows to select them all.
6. Press the Delete key.

Tada! The records you selected are deleted. That code binds a DataTable to the grid. The DataTable is created and populated manually by the code but it could come from anywhere.
 
Yes it does. If it didn't work for you then you did it wrong.This has got nothing to do with wizards. You simply bind a DataTable to the grid and it works. Follow these instructions to see for yourself:

1. Create a new Windows Forms Application project.
2. Add a DataGridView to the form and set its Dock property to Fill.
3. Add the following code to the form:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.DataSource = GetData()
End Sub

Private Function GetData() As DataTable
    Dim table As New DataTable

    With table.Columns
        .Add("ID", GetType(Integer))
        .Add("Name", GetType(String))
    End With

    With table.Rows
        .Add(1, "Peter")
        .Add(2, "Paul")
        .Add(3, "Mary")
        .Add(4, "John")
    End With

    Return table
End Function
4.Run the project.
5. Ctrl+Click multiple rows to select them all.
6. Press the Delete key.

Tada! The records you selected are deleted. That code binds a DataTable to the grid. The DataTable is created and populated manually by the code but it could come from anywhere.

Thanks a lot for your comment!

now I see what you meant there.
could you please look at my previous post and see what I asked about the SelectedRows method?
that would help me a lot ^^.

have a nice day and thanks for everything ^_^
 
Just like all controls, you don't delete rows from a DataGridView directly if it is bound to a data source. If you want to do this in code (which is rather pointless if you ask me) then you need to loop through SelectedRows and get the DataBoundItem of each one first. You have to get all the bound items first because the SelectedRows is going to change as soon as you remove one. You can then loop through the bound items and remove each one from the data source. If you bind the DataTable to the grid via a BindingSource then you can call the Remove method of the BindingSource and that will Delete the underlying DataRow and the corresponding row will be removed from the grid. That's exactly what happens by default when you press the Delete key but you don;t have to write any of the code yourself.
 
Just like all controls, you don't delete rows from a DataGridView directly if it is bound to a data source. If you want to do this in code (which is rather pointless if you ask me) then you need to loop through SelectedRows and get the DataBoundItem of each one first. You have to get all the bound items first because the SelectedRows is going to change as soon as you remove one. You can then loop through the bound items and remove each one from the data source. If you bind the DataTable to the grid via a BindingSource then you can call the Remove method of the BindingSource and that will Delete the underlying DataRow and the corresponding row will be removed from the grid. That's exactly what happens by default when you press the Delete key but you don;t have to write any of the code yourself.

Thanks for your reply!
Well, if you look a few posts back, you will see that I wrote a loop do delete the selected rows and it worked! All was needed is 3 lines.
and to do that, I used the SelectedRows method. The problem is that I was trying many things and then this loop worked, but I don't actually understand what I did there and I would like to get an explanation if you can provide me one^^

have a nice day!
 
Back
Top