How to Use a CheckBox & a ComboBox in a DataGridView

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use a CheckBox & a ComboBox in a DataGridView
Remove rows from a DataGridView control in Visual Basic. (Jupiterimages/Photos.com/Getty Images)

Using a CheckBox and a ComboBox in your DataGridView control can give the user a second level of security to the data before deleting a row. You can use a DataGridView control in Visual Basic.NET (VB.NET). In VB.NET, you can use the "DataGridViewComboBoxColumn" and "DataGridViewCheckBoxColumn" classes to dynamically add these column types to the DataGridView control. Use the ComboBox column to give the row a value such as "True," and allow the user to check the rows they want to delete by using the CheckBox column.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Microsoft Visual Basic Express

Show MoreHide

Instructions

  1. 1

    Launch Microsoft Visual Basic Express. Click "New Project" on the left pane of your computer screen, and then double-click "Microsoft Windows Forms" to create a new project.

  2. 2

    Double-click "DataGridView" to add a new control to the form. Double-click "Button" to add a new button to the form. Add a second button using the same technique.

  3. 3

    Double-click "Button1" to create a button click event. Add the following code to add three new columns to the DataGridView control:

    DataGridView1.ColumnCount = 3

        DataGridView1.Columns(0).Name = "ID"
    
        DataGridView1.Columns(1).Name = "Car_Name"
    
        DataGridView1.Columns(2).Name = "Car_Price"
    
  4. 4

    Add three rows of data by adding the following code:

        Dim row As String() = New String() {"1", "Car 1", "1000"}
    
        DataGridView1.Rows.Add(row)
    
        row = New String() {"2", "Car 2", "2000"}
    
        DataGridView1.Rows.Add(row)
    
        row = New String() {"3", "Car 3", "3000"}
    
        DataGridView1.Rows.Add(row)
    
        row = New String() {"4", "Car 4", "4000"}
    
        DataGridView1.Rows.Add(row)
    
  5. 5

    Add a ComboBox column by adding the following code:

        Dim cmb As New DataGridViewComboBoxColumn()
    
        cmb.HeaderText = "Select"
    
        cmb.Name = "cmb"
    
        cmb.MaxDropDownItems = 4
    
        cmb.Items.Add("True")
    
        cmb.Items.Add("False")
    
        DataGridView1.Columns.Add(cmb)
    
  6. 6

    Add a CheckBox column by adding the following code:

        Dim chk As New DataGridViewCheckBoxColumn()
    
        chk.HeaderText = "Check Row"
    
        chk.Name = "ckBox"
    
        DataGridView1.Columns.Add(chk)
    
  7. 7

    Switch back to the form design. Double-click "Button2" to create a new click event. Add the following code to remove any rows checked in the DataGridView:

    For Each row As DataGridViewRow In DataGridView1.Rows

            If row.Cells("cmb").Value = "True" Then
    
                If row.Cells("ckBox").Value Then
    
                    DataGridView1.Rows.Remove(row)
    
                End If
    
            End If
    
        Next
    
  8. 8

    Press "F5" to run the program, and click "Button1" to populate the DataGridView control. Check all the rows by clicking on each check box. Set the ComboBox column to "True" if you want that row to be deleted.

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.