Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-select if Checked

I am not able to add Checkbox in column header to multi-select(select all) Rows of Datagridview, I google it but they are giving me to add checkbox in row not in column header.

So that I'll select all check box column header click. Please look at the image belowfor example. it is the listview image that i got form internet But I'm using Datagridview. enter image description here

like image 382
Rahul Shirphule Avatar asked Jun 16 '26 23:06

Rahul Shirphule


1 Answers

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

Usage:

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

I hope it will helps

like image 145
spajce Avatar answered Jun 19 '26 18:06

spajce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!