VB.NET - How To Remove The Checked Row From Datagridview Using VB NET
In This VB.NET Tutorial We Will See How To Delete The DataGridView Row With CheckBox ( Checked Cell Is Checked ) In VBNET Programming Language.
Project Source Code:
Public Class VB_delete_datagridview_Row_checked_checkbox_cell
Private Sub VB_delete_datagridview_Row_checked_checkbox_cell_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'create a text columns
Dim dgvcId As New DataGridViewTextBoxColumn()
dgvcId.HeaderText = "Id"
Dim dgvcFn As New DataGridViewTextBoxColumn()
dgvcFn.HeaderText = "First Name"
Dim dgvcLn As New DataGridViewTextBoxColumn()
dgvcLn.HeaderText = "Last Name"
'create checkbox column
Dim dgvcCheckBox As New DataGridViewCheckBoxColumn()
dgvcCheckBox.HeaderText = "Delete"
'add columns to datagridview
DataGridView1.Columns.Add(dgvcId)
DataGridView1.Columns.Add(dgvcFn)
DataGridView1.Columns.Add(dgvcLn)
DataGridView1.Columns.Add(dgvcCheckBox)
' add some row to datagridview
' true the checkBox Is checked
' false the checkBox Is Not checked
DataGridView1.Rows.Add("1", "DTYUI", "RTYUI", False)
DataGridView1.Rows.Add("2", "KJHGF", "OIUY", False)
DataGridView1.Rows.Add("3", "SDFGH", "LTDFGH", False)
DataGridView1.Rows.Add("4", "ERJHGV", "CVHEYH", False)
DataGridView1.Rows.Add("5", "AQSFGHKK", "TRDFHJH", False)
DataGridView1.Rows.Add("6", "NQAAZTYI", "KITGHKG", False)
DataGridView1.Rows.Add("7", "XBVSARY", "OTYHNC", False)
DataGridView1.Rows.Add("8", "ACJKT", "PMLGESB", False)
DataGridView1.Rows.Add("9", "FGHKL", "IUYTEYK", False)
DataGridView1.Rows.Add("10", "WQAZR", "ERTYUVBD", False)
DataGridView1.Rows.Add("11", "CKKTS", "EGCQA", False)
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DataGridView1.AllowUserToAddRows = False
End Sub
' button delete checked row
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
For i As Integer = DataGridView1.Rows.Count() - 1 To 0 Step -1
Dim delete As Boolean
delete = DataGridView1.Rows(i).Cells(3).Value
' if the checkbox cell is checked
If delete = True Then
Dim row As DataGridViewRow
row = DataGridView1.Rows(i)
DataGridView1.Rows.Remove(row)
End If
Next
End Sub
End Class
///////////////OUTPUT:
Download Projects Source Code