Private Sub AddSelectAllCheckBox(ByVal dgv As DataGridView)
Dim cbx As New CheckBox
cbx.Name = "SelectAll"
cbx.Size = New Size(14, 14)
Dim rect As Rectangle
rect = dgv.GetCellDisplayRectangle(0, -1, True)
cbx.Location = New System.Drawing.Point(rect.Location.X + ((rect.Width - cbx.Width) / 2), rect.Location.Y + ((rect.Height - cbx.Height) / 2))
dgv.Controls.Add(cbx)
AddHandler cbx.Click, AddressOf HeaderCheckBox_Click
End Sub
Private Sub HeaderCheckBox_Click(ByVal sender As Object, ByVal e As EventArgs)
If DataGridView_Discre.Rows.Count > 0 Then
Dim cbx As CheckBox
cbx = DirectCast(sender, CheckBox)
Dim dgv As DataGridView = DataGridView
DataGridView.CurrentCell = DataGridView.Rows(DataGridView.Rows.Count - 1).Cells(0)
For Each row As DataGridViewRow In dgv.Rows
row.Cells(0).Value = cbx.Checked
Next
DataGridView.CurrentCell = DataGridView.Rows(0).Cells(0)
End If
End Sub
Design UI is as follow:
效果图: