Link to home
Create AccountLog in
Avatar of ty-exexch-ko
ty-exexch-koFlag for Albania

asked on

Remove/Delete row from DataGridView

How I can remove/delete row or set of rows from DataGridView ?
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

Hello,

Write code in rowdeleting event of a gridview. or use this code.

http://www.codeproject.com/KB/webforms/GridViewConfirmDelete.aspx
You can use the .Rows.Remove either in the DataGridView or the DataTable which is being used as the datasource.
Avatar of ty-exexch-ko

ASKER

I want to know how delete row of DataGridView in Vb.NET
That's what I said above.
try this

Use the Rows collection's Remove or RemoveAt method. The following code checks to see if the current row is new because you cannot delete a new row. It then removes the current row.  
 
Private Sub btnDelete_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnDelete.Click
    If Not DataGridView1.CurrentRow.IsNewRow Then
        DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
    End If
End Sub


You select the entire row by clicking the header on the right of the DataGridView.
Then  you can use the following code to remove a row(s) from the control. You can select more than one row for deletion at a time.

// remove all rows selected  
 For Each dr As DataGridViewRow In DataGridView1.SelectedRows
      If DataGridView1.Rows.Count > 1 Then
            DataGridView1.Rows.Remove(dr)
      End If
Next

 

To delete all rows:
DataGridView1.Rows.Clear()

To delete Selected ROW:
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)

To delete Selected ROWS:
        For i As Integer = 0 To DataGridView1.SelectedRows.Count - 1
            DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
        Next

Thanks for all answers

I need for example to delete the 2nd row from Datagridview without select the row ?
ASKER CERTIFIED SOLUTION
Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account