简体   繁体   English

如何在DataGridView的标题单元格中添加复选框

[英]How to add a CheckBox in a Header cell in a DataGridView

How can I add a CheckBox in the Header ? 如何在Header添加CheckBox It will then select all the CheckBoxes . 然后它将选择所有CheckBoxes
Also, how can I align the CheckBox in center. 另外,如何将CheckBox对准中心。

Here's my Code: 这是我的代码:

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

Heres the image: 继承人的形象:

在此处输入图片说明

如果我记得很好:

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

To create a customised header cell (one containing the CheckBox , and supporting functionality) you will need to create three classes. 要创建自定义的标头单元(一个包含CheckBox和支持功能),您将需要创建三个类。

  1. DataGridViewCheckBoxHeaderCell.vb 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(eX + CellLocation.X, eY + CellLocation.Y) If (pX >= CheckBoxLocation.X AndAlso pX <= CheckBoxLocation.X + CheckBoxSize.Width AndAlso pY >= CheckBoxLocation.Y AndAlso pY <= 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 
  2. DataGridViewCheckBoxEventArgs.vb 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 
  3. DataGridViewCustomCheckBoxColumn.vb 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 

and finally a demo/testing form to illustrate how it all works together. 最后是演示/测试表格,以说明它们如何协同工作。 After dropping a DataGridView on to your form, use this code to quickly fill it with test data comprised of four columns. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM