How to Use Checkbox in GridView

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use Checkbox in GridView
Add a Checkbox to a Data Grid View control using VB.NET. (stock image of earth in binary code net image by Ruslana Stovner from Fotolia.com)

Knowing how to create and use a Checkbox control inside a DataGridView control can provide the user with more flexibility. You can use Microsoft Visual Basic.NET (VB.NET) to create a Windows application that can use the DataGridView and Checkbox controls. VB.NET is an object-oriented computer programming language that is relatively easy to use. A DataGridView control displays data in a tabular format similar to a database table. A Checkbox control can be used to give the user the option to select rows.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Microsoft Visual Basic Express

Show MoreHide

Instructions

  1. 1

    Start Microsoft Visual Basic Express, click "New Project..." on the left pane of your screen then select "Windows Forms Application." Click "OK."

  2. 2

    Double-click "Button" on the "Toolbox" pane to add a new button control. Double-click "DataGridView" to add a new grid control. Right-click the "DataGridView" control, select "Add Column..." to open the "Add Column" Window.

  3. 3

    Select "DataGridViewCheckBoxColumn" next to "Type:" and click "Add." Click "Close." You have just added the first column to include check boxes.

  4. 4

    Double-click "Button1" to open the "Form1.vb" module. Press "Ctrl" and "A," then press "Delete" to remove all existing code.

  5. 5

    Copy and paste the following code to your "Form1.vb" module to add data to your DataGridView control and identify what check boxes are selected.

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    
                            Handles MyBase.Load
    
    
    
        DataGridView1.ColumnCount = 3
    
        DataGridView1.RowCount = 5
    
        With DataGridView1
    
            .Columns(1).Name = "Last Name"
    
            .Columns(2).Name = "Position"
    
    
    
            .Item(1, 0).Value = "Gonzalez"
    
            .Item(1, 1).Value = "Lopez"
    
            .Item(1, 2).Value = "Garza"
    
            .Item(1, 3).Value = "Ramos"
    
    
    
            .Item(2, 0).Value = "Doctor"
    
            .Item(2, 1).Value = "Engineer"
    
            .Item(2, 2).Value = "Teacher"
    
            .Item(2, 3).Value = "Manager"
    
        End With
    
    
    
    End Sub
    
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim tmpStr As String
    
        For Each row As DataGridViewRow In DataGridView1.Rows
    
            If DataGridView1.Rows(row.Index).Cells(0).Value = True Then
    
                tmpStr = tmpStr & row.Index & ","
    
            End If
    
        Next
    
    
    
        If tmpStr <> "" Then
    
            tmpStr = tmpStr.Substring(0, tmpStr.Length - 1)
    
            MsgBox(tmpStr & " rows are checked!")
    
        End If
    
    
    
    End Sub
    

    End Class

  6. 6

    Press "F5" to run your application, then press "Button1" to show a message box with rows selected.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.