如果选中,则选择多选

时间:2013-02-08 07:26:29

标签: vb.net winforms datagridview checkbox

我无法将列标题中的Checkbox添加到Datagridview的多选(全选)行,我 google 但它们会让我添加行中的checkbox不在列标题中。

这样我就会选中所有复选框列标题点击。 请查看下面的图片。它是我从互联网上获得的列表视图图像但是我正在使用Datagridviewenter image description here

1 个答案:

答案 0 :(得分:2)

我是从互联网上得到的,但我不记得链接在哪里,最初来自c#。

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

<强>用法:

   Sub Form1Load(sender As Object, e As EventArgs) Handles MyBase.Load
        show_chkBox()
   End Sub

我希望它会有所帮助