Private Sub CheckAllCheckBoxes(ByVal checkAll As Boolean)
For Each row As DataGridViewRow In DataGridView1.Rows
Dim chkBox As DataGridViewCheckBoxCell = CType(row.Cells("CheckBoxColumn"), DataGridViewCheckBoxCell)
chkBox.Value = checkAll
Next
End Sub
Private Sub btnCheckAll_Click(sender As Object, e As EventArgs) Handles btnCheckAll.Click
CheckAllCheckBoxes(True)
End Sub
Private Sub btnUncheckAll_Click(sender As Object, e As EventArgs) Handles btnUncheckAll.Click
CheckAllCheckBoxes(False)
End Sub
Private Sub SaveCheckedItems()
Using connection As New SqlConnection("YourConnectionString")
connection.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
Dim isChecked As Boolean = Convert.ToBoolean(row.Cells("CheckBoxColumn").Value)
Dim itemId As Integer = Convert.ToInt32(row.Cells("ItemIdColumn").Value)
Dim command As New SqlCommand("INSERT INTO YourTable (ItemId, IsChecked) VALUES (@ItemId, @IsChecked)", connection)
command.Parameters.AddWithValue("@ItemId", itemId)
command.Parameters.AddWithValue("@IsChecked", isChecked)
command.ExecuteNonQuery()
Next
End Using
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
SaveCheckedItems()
End Sub