Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > How to insert the 'Select all' feature in the datagridview?
 

How to insert the 'Select all' feature in the datagridview?

Hi all,

Iam displaying the data in the datagidview and wanted to select all the data using the checked box. I can only select one by one but not the select all.

Hope that anyone could help.

Thanks
LiL_Is  Wednesday, November 18, 2009 7:31 AM
A.m.a.L - aditi.com - Think Product  Wednesday, November 18, 2009 7:43 AM
Hi all,

Iam displaying the data in the datagidview and wanted to select all the data using the checked box. I can only select one by one but not the select all.

Hope that anyone could help.

Thanks

try it

If Checkbox1.Checked then

If Button1.Focus Then

Me.DataGridView1.MultiSelect = True

Me.DataGridView1.SelectAll()

end if


Don't judge me, just Upgrade me. Thanks!
  • Marked As Answer byLiL_Is Monday, November 30, 2009 5:00 AM
  •  
Malange  Friday, November 27, 2009 8:25 PM
Do you mean you want to put a checkbox in column header and want to select gridview using checkbox column?

An example below adds a checkbox in column header(in middle widthwise) and selects rows based on its checked value. I am adding checkbox to datagridview's 3rd column headerin form's load event




  Private Function GetLocation(ByVal ColIndex As Integer, ByVal cb As CheckBox) As Point
        Dim cntrlLoc = DataGridView1.GetCellDisplayRectangle(ColIndex, -1, True).Location
        Return New Point(cntrlLoc.X + (DataGridView1.Columns(ColIndex).Width / 2 - cb.Width / 2), cntrlLoc.Y + 4)

    End Function

    Private Sub DataGridView1_ColumnWidthChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
        Dim cb As CheckBox = CType(DataGridView1.Controls("cbSelectAll"), CheckBox)
        cb.Location = GetLocation(2, cb) '2 is index of third column
    End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ColInd = 2 ' adding checkbox to third column)
        Dim cb As New CheckBox With {.Name = "cbSelectAll", .Size = New Size(14, 14)}
        cb.Location = GetLocation(ColInd, cb)
        DataGridView1.Controls.Add(cb)
        AddHandler cb.CheckedChanged, AddressOf CB_CheckChanged
    End Sub
Private Sub CB_CheckChanged(ByVal s As Object, ByVal e As System.EventArgs)
        Dim ColInd = 2 ' changing checked value for third column
        For Each dr As DataGridViewRow In DataGridView1.Rows
            dr.Cells(ColInd).Value = CType(s, CheckBox).Checked
        Next
    End Sub
<br/>
  • Marked As Answer byLiL_Is Monday, November 30, 2009 5:00 AM
  •  
Arjun Paudel  Saturday, November 28, 2009 7:46 AM
A.m.a.L - aditi.com - Think Product  Wednesday, November 18, 2009 7:43 AM
Hi all,

Iam displaying the data in the datagidview and wanted to select all the data using the checked box. I can only select one by one but not the select all.

Hope that anyone could help.

Thanks

try it

If Checkbox1.Checked then

If Button1.Focus Then

Me.DataGridView1.MultiSelect = True

Me.DataGridView1.SelectAll()

end if


Don't judge me, just Upgrade me. Thanks!
  • Marked As Answer byLiL_Is Monday, November 30, 2009 5:00 AM
  •  
Malange  Friday, November 27, 2009 8:25 PM
Do you mean you want to put a checkbox in column header and want to select gridview using checkbox column?

An example below adds a checkbox in column header(in middle widthwise) and selects rows based on its checked value. I am adding checkbox to datagridview's 3rd column headerin form's load event




  Private Function GetLocation(ByVal ColIndex As Integer, ByVal cb As CheckBox) As Point
        Dim cntrlLoc = DataGridView1.GetCellDisplayRectangle(ColIndex, -1, True).Location
        Return New Point(cntrlLoc.X + (DataGridView1.Columns(ColIndex).Width / 2 - cb.Width / 2), cntrlLoc.Y + 4)

    End Function

    Private Sub DataGridView1_ColumnWidthChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
        Dim cb As CheckBox = CType(DataGridView1.Controls("cbSelectAll"), CheckBox)
        cb.Location = GetLocation(2, cb) '2 is index of third column
    End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ColInd = 2 ' adding checkbox to third column)
        Dim cb As New CheckBox With {.Name = "cbSelectAll", .Size = New Size(14, 14)}
        cb.Location = GetLocation(ColInd, cb)
        DataGridView1.Controls.Add(cb)
        AddHandler cb.CheckedChanged, AddressOf CB_CheckChanged
    End Sub
Private Sub CB_CheckChanged(ByVal s As Object, ByVal e As System.EventArgs)
        Dim ColInd = 2 ' changing checked value for third column
        For Each dr As DataGridViewRow In DataGridView1.Rows
            dr.Cells(ColInd).Value = CType(s, CheckBox).Checked
        Next
    End Sub
<br/>
  • Marked As Answer byLiL_Is Monday, November 30, 2009 5:00 AM
  •  
Arjun Paudel  Saturday, November 28, 2009 7:46 AM

You can use google to search for other answers

Custom Search

More Threads

• How to do Image Annotation using vb.net ?
• How to stop loading the same form in mdi?
• Simple method problems
• Getting the Windows user name in Terminal Server / Citrix
• Overload Resolution Problem
• New row in datagrid: Old values are overwritten instead of a new row appears
• "Show All Files" option in Visual Studio
• Simple question about simple report (not Crystal report) please
• where to locate DLLs
• Datagrid value change