I am learning vb.net from tutorial sites, book and having trouble to delete row from datagrid view. can someone help me how to delete a selected row in datagrid on click of a button .Thank you

the error i get : 'user' is not declared

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim con As SqlCeConnection = New SqlCeConnection("DataSource=C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\samples\Northwind.sdf")
      
  Dim CmdStr As String
        Dim dr As DataRow
        Dim cmd As SqlCeCommand = New SqlCeCommand(CmdStr, con)
        'Dim cmd As  
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("Done")
        dr = users.rows.item(users.currentrow.index)
        dr.Delete()
        dtusers.acceptchanges()
        CmdStr = "Delete* from [users] where  ID = " + DataGridView1.CurrentRow.Cells(0).Value + ""

 End Sub

Hi,

You can delete rows by first selecting the rows and then pressing the Delete key. If you may want to confirm the deletion with the user before deleting them. You can do this with the UserDeletingRow event:

Private Sub DataGridView1_UserDeletingRow( _
       ByVal sender As Object, _
       ByVal e As System.Windows.Forms. _
       DataGridViewRowCancelEventArgs) _
       Handles DataGridView1.UserDeletingRow
        If (Not e.Row.IsNewRow) Then
            Dim response As DialogResult = _
            MessageBox.Show( _
            "Are you sure you want to delete this row?", _
            "Delete row?", _
            MessageBoxButtons.YesNo, _
            MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2)
            If (response = DialogResult.No) Then
                e.Cancel = True
            End If
        End If
    End Sub

Thank you for quick response.if you have time i would like to ask you how do i save the changes in table ? i mean i want to delete row not only from datagridview but also from table at the same time.

Thanks , I got it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.