Deleting a record from the datagridview

前端 未结 2 1570
無奈伤痛
無奈伤痛 2021-01-16 21:37

In VB2012, I have a created a student management software with SQL2012 as the back-end. The below code is used for deleting a selected record from the datagridview when DELE

相关标签:
2条回答
  • 2021-01-16 22:17

    the code below is also helpful

     Private Sub btnRemoveItem_Click(sender As Object, e As EventArgs) Handles btnRemoveItem.Click
            If datagridviewname.SelectedRows.Count > 0 Then
                For i As Integer = datagridviewname.SelectedRows.Count - 1 To 0 Step -1
                    datagridviewname.Rows.RemoveAt(datagridviewname.SelectedRows(i).Index)
                Next
            Else
                MessageBox.Show("Select Row to remove")
            End If
        End Sub
    
    0 讨论(0)
  • 2021-01-16 22:31

    Selected row is not necessary current, from the perspective of binding source. You need to be using DataGridView.SelectedRows. Each is a DataGridViewRow, and you should be able to retrieve DataGridViewRow.DataBoundItem, cast it to DataRow, and use Delete on that.

    0 讨论(0)
提交回复
热议问题