2

我试图在特定的 datagridview 列标题中添加一个复选框,我在网上找到了一些代码来提供帮助,但它没有正确对齐,我不确定如何修复它。

下面是问题的图片和代码,任何帮助将不胜感激!

PS我认为这可能与属性有关,但我已经玩过它们但没有成功。

在此处输入图像描述

Private checkboxHeader231 As CheckBox
Private Sub show_chkBox()
Dim rect As Rectangle = DataGridView1.GetCellDisplayRectangle(columnIndexOfCheckBox, -1, True)
' set checkbox header to center of header cell. +1 pixel to position 
rect.Y = 3
rect.X = rect.Location.X + 8 + (rect.Width / 4)
checkboxHeader231 = New CheckBox()
With checkboxHeader231
    .BackColor = Color.Transparent
End With

checkboxHeader231.Name = "checkboxHeader1"
checkboxHeader231.Size = New Size(18, 18)
checkboxHeader231.Location = rect.Location
AddHandler checkboxHeader231.CheckedChanged, AddressOf checkboxHeader231_CheckedChanged
DataGridView1.Controls.Add(checkboxHeader231)
End Sub

Private Sub checkboxHeader231_CheckedChanged(sender As System.Object, e As System.EventArgs)
Dim headerBox As CheckBox = DirectCast(DataGridView1.Controls.Find("checkboxHeader1", True)(0), CheckBox)
For Each row As DataGridViewRow In DataGridView1.Rows
    row.Cells(columnIndexOfCheckBox).Value = headerBox.Checked
Next
End Sub
4

2 回答 2

2

这是我的第一个条目,但我认为这就是你要找的。我对其进行了测试,它适用于我的数据网格。您使用的是矩形的宽度,您将需要它来代替列宽。我将列标题设置为 4,但是您可以将 4 替换为您要使用的列,我以两种方式放置它,一种是四循环,另一种是单行。告诉我这是否对您有用:

Dim rect As Rectangle = DataGridView1.GetCellDisplayRectangle(4, -1, True) ' replace 4
        rect.Y = 3

        Dim sum = DataGridView1.Columns(0).Width

        'for this area write a for loop to find the width of each column except for the last line which you manually do
        '
        '
        'For i As Integer = 1 To 4 - 1 Step 1  ' replace 4
        'sum = sum + DataGridView1.Columns(i).Width
        'Next

        sum = sum + DataGridView1.Columns(1).Width
        sum = sum + DataGridView1.Columns(2).Width
        sum = sum + DataGridView1.Columns(3).Width
        ' stop here and add the last line by hand here

        sum = sum + (DataGridView1.Columns(4).Width / 2) + 35 ' used in both cases ' replace 4
        rect.X = sum

        checkboxHeader231 = New CheckBox()
        With checkboxHeader231
            .BackColor = Color.Transparent
        End With

        checkboxHeader231.Name = "checkboxHeader1"
        checkboxHeader231.Size = New Size(18, 18)
        checkboxHeader231.Location = rect.Location
        AddHandler checkboxHeader231.CheckedChanged, AddressOf checkboxHeader231_CheckedChanged
        DataGridView1.Controls.Add(checkboxHeader231)
于 2013-07-18T15:55:57.307 回答
0
Private headerBox As CheckBox
Private Sub show_checkBox()
        Dim checkboxHeader As CheckBox = New CheckBox()
        Dim rect As Rectangle = PendingApprovalServiceListingDataGridView.GetCellDisplayRectangle(4, -1, True)
        rect.X = 20
        rect.Y = 12
        With checkboxHeader
            .BackColor = Color.Transparent
        End With
        checkboxHeader.Name = "checkboxHeader"
        checkboxHeader.Size = New Size(14, 14)
        checkboxHeader.Location = rect.Location
        AddHandler checkboxHeader.CheckedChanged, AddressOf checkboxHeader_CheckedChanged
        PendingApprovalServiceListingDataGridView.Controls.Add(checkboxHeader)
    End Sub
    Private Sub checkboxHeader_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        headerBox = DirectCast(PendingApprovalServiceListingDataGridView.Controls.Find("checkboxHeader", True)(0), CheckBox)
        For Each row As DataGridViewRow In PendingApprovalServiceListingDataGridView.Rows
            row.Cells(0).Value = headerBox.Checked
        Next
    End Sub
于 2018-07-04T04:02:37.393 回答