vb.net 将复选框添加到 datagridview 列标题,未正确对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15535398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:54:02  来源:igfitidea点击:

Adding checkbox to datagridview column header, not aligning properly

vb.netwinformscheckboxdatagridview

提问by Robin L

Im trying to add a checkbox to a specific datagridview column header, I found some code online to help but it's not aligning properly and I'm not really sure how to fix it.

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

Below is an image of the problem and the code, any help would be greatly appreciated!

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

P.S. I think it might be something to do with properties but I've played around with them but not been successful.

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

enter image description here

enter image description here

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

回答by user2596328

This is my first entry, but I think this is what youre looking for. I tested it and it worked on my datagrid. You were using the width for the rectangle, youll need it for the column width instead. I set the column header to 4, but you would replace the 4 with your column you want to use I put it in two ways, one with a four loop, the other just as single lines. Tell me if this worked for you:

这是我的第一个条目,但我认为这正是您要找的。我测试了它,它在我的数据网格上工作。您使用的是矩形的宽度,而您将需要它作为列宽。我将列标题设置为 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)

回答by Amuthan Balasubramanian

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