Multi-select if Checked

I cant add Checkbox

to column header for multiple select (select all) Rows Datagridview

, I google, but they give me add Checkbox

to row not containing column header.

So, I will select all the column header of the checkbox column. Please take a look at the image below for example. this is an image of a list that I got online. But I am using Datagridview

. enter image description here

+3


source to share


1 answer


I got this from the internet, but I can't remember where the link is and originally comes from 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

      

Application:



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

      

I hope this helps

+2


source







All Articles