Rahul Shirphule
Rahul Shirphule

Reputation: 999

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

Upvotes: 3

Views: 2103

Answers (1)

spajce
spajce

Reputation: 7092

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

Upvotes: 2

Related Questions