如何在datagridview的头单元格中添加复选框

oewdyzsn  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(336)

如何添加 CheckBoxHeader ? 然后它将选择所有 CheckBoxes .
另外,如何对齐 CheckBox 在中间。
这是我的密码:

Private Sub frm_reciev_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim chkbox As New DataGridViewCheckBoxColumn

    With chkbox
        .Width = 60
    End With

    With DataGridView1
        .Columns.Add(chkbox)
        .RowHeadersVisible = False
    End With
End Sub

图片如下:

8yparm6h

8yparm6h1#

如果我记得很清楚:

dataGridView1.Columns[ColumnName].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
2w3kk1z5

2w3kk1z52#

创建自定义标题单元格(包含 CheckBox ,以及支持的功能)您将需要创建三个类。
datagridviewcheckboxheadercell.vb文件

Imports System.Windows.Forms.VisualStyles
Public Class DataGridViewCheckBoxHeaderCell
    Inherits DataGridViewColumnHeaderCell
    Public Event CheckBoxClicked As EventHandler(Of DataGridViewCheckBoxHeaderCellEventArgs)

    Private Property CheckBoxLocation As Point
    Private Property CheckBoxSize As Size
    Private Property IsChecked As Boolean
    Private Property CellLocation As Point
    Private Property CheckBoxState As CheckBoxState

    Protected Overrides Sub Paint(graphics As Graphics,
                          clipBounds As Rectangle,
                          cellBounds As Rectangle,
                          rowIndex As Integer,
                          dataGridViewElementState As DataGridViewElementStates,
                          value As Object,
                          formattedValue As Object,
                          errorText As String,
                          cellStyle As DataGridViewCellStyle,
                          advancedBorderStyle As DataGridViewAdvancedBorderStyle,
                          paintParts As DataGridViewPaintParts)
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)

        CheckBoxSize = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal)

        Dim x As Integer = cellBounds.Location.X + (cellBounds.Width / 2) - (CheckBoxSize.Width / 2)
        Dim y As Integer = cellBounds.Location.Y + (cellBounds.Height / 2) - (CheckBoxSize.Height / 2)

        CheckBoxLocation = New Point(x, y)

        CellLocation = cellBounds.Location

        CheckBoxState = If(IsChecked, CheckBoxState.CheckedNormal, CheckBoxState.UncheckedNormal)

        CheckBoxRenderer.DrawCheckBox(graphics, CheckBoxLocation, CheckBoxState)
    End Sub

    Protected Overrides Sub OnMouseClick(e As DataGridViewCellMouseEventArgs)
        Dim p As Point = New Point(e.X + CellLocation.X, e.Y + CellLocation.Y)

        If (p.X >= CheckBoxLocation.X AndAlso
            p.X <= CheckBoxLocation.X + CheckBoxSize.Width AndAlso
            p.Y >= CheckBoxLocation.Y AndAlso
            p.Y <= CheckBoxLocation.Y + CheckBoxSize.Height) Then

            IsChecked = Not IsChecked
            Dim eventArgs As New DataGridViewCheckBoxHeaderCellEventArgs(e.ColumnIndex, IsChecked)
            OnCheckBoxClicked(eventArgs)
            Me.DataGridView.InvalidateCell(e.ColumnIndex, e.RowIndex)
        End If

        MyBase.OnMouseClick(e)
    End Sub

    Protected Overridable Sub OnCheckBoxClicked(eventArgs As DataGridViewCheckBoxHeaderCellEventArgs)
        RaiseEvent CheckBoxClicked(Me, eventArgs)
    End Sub
End Class

datagridviewcheckboxeventargs.vb文件

Public Class DataGridViewCheckBoxHeaderCellEventArgs
    Inherits EventArgs

    Public Sub New(columnIndex As Integer, isChecked As Boolean)
        Me.ColumnIndex = columnIndex
        Me.IsChecked = isChecked
    End Sub

    Public Property ColumnIndex As Integer
    Public Property IsChecked As Boolean
End Class

datagridviewcustomcheckboxcolumn.vb

Public Class DataGridViewCustomCheckBoxColumn
    Inherits DataGridViewCheckBoxColumn

    Public Sub New()
        MyBase.New()

        MyBase.CellTemplate = New DataGridViewCheckBoxCell()

        Dim datagridViewCheckBoxHeaderCell As New DataGridViewCheckBoxHeaderCell()

        Me.HeaderCell = datagridViewCheckBoxHeaderCell
        Me.Width = 50

        AddHandler datagridViewCheckBoxHeaderCell.CheckBoxClicked, AddressOf datagridViewCheckBoxHeaderCell_OnCheckBoxClicked
    End Sub

    Private Sub datagridViewCheckBoxHeaderCell_OnCheckBoxClicked(sender As Object, e As DataGridViewCheckBoxHeaderCellEventArgs)
        DataGridView.RefreshEdit()

        For Each row As DataGridViewRow In Me.DataGridView.Rows
            If (Not row.Cells(e.ColumnIndex).ReadOnly) Then
                row.Cells(e.ColumnIndex).Value = e.IsChecked
            End If
        Next

        DataGridView.RefreshEdit()
    End Sub

    Public Overrides Property CellTemplate As DataGridViewCell
        Get
            Return MyBase.CellTemplate
        End Get
        Set
            ' Ensure that the cell used for the template is a DataGridViewCheckBoxCell.
            If (Value IsNot Nothing) Then
                Throw New InvalidCastException("Must be a DataGridViewCheckBoxCell")
            End If

            MyBase.CellTemplate = Value
        End Set
    End Property
End Class

最后是一个演示/测试表单,以说明它们是如何协同工作的。在丢了一个 DataGridView 在您的表单上,使用下面的代码快速填充由四列组成的测试数据。

Public Class Form1
        Public Sub New()

            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            InitialiseData()

            DataGridView1.AutoGenerateColumns = False
            DataGridView1.Columns.Clear()

            ' This is the important line that will add a customised Checkbox colum to the DataGridView
            Dim checkedColumn As New DataGridViewCustomCheckBoxColumn()
            checkedColumn.DataPropertyName = "Checked"
            DataGridView1.Columns.Add(checkedColumn)

            Dim nameColumn As New DataGridViewTextBoxColumn()
            nameColumn.DataPropertyName = "Name"
            DataGridView1.Columns.Add(nameColumn)

            Dim addressColumn As New DataGridViewTextBoxColumn()
            addressColumn.DataPropertyName = "Address"
            DataGridView1.Columns.Add(addressColumn)

            Dim phoneColumn As New DataGridViewTextBoxColumn()
            phoneColumn.DataPropertyName = "Phone"
            DataGridView1.Columns.Add(phoneColumn)

            DataGridView1.DataSource = People
        End Sub

        Private People As DataTable

        Private Sub InitialiseData()
            People = New DataTable()
            People.Columns.Add("Checked", GetType(Boolean))
            People.Columns.Add("Name", GetType(String))
            People.Columns.Add("Address", GetType(String))
            People.Columns.Add("Phone", GetType(String))

            People.Rows.Add(True, "J1", "Address1", "123")
            People.Rows.Add(False, "J2", Nothing, "456")
            People.Rows.Add(True, "J3", "Address3", "789")
            People.Rows.Add(False, "J4", "Address4", "147")
            People.Rows.Add(DBNull.Value, "J5", "Address5", "258")
        End Sub
    End Class

相关问题