Help with adding checkbox column to DataGridView in window form

74,281

Solution 1

Add new column in the properties of the DataGridView by:

  1. Choosing Columns from properties panel and double click on it
  2. then choose " Add... " button
  3. then set the new column as " Unbound Column "
  4. Give it a name and choose its type " DataGridViewCheckBoxColumn "
  5. Set the header you want and make sure that " read only " is not selected.

that's it.

(If the database field (in SQL Server) is of type 'bit' then the the datagridview automatically maps it to the datagridview as a checkbox instead of a textbox. No coding necessary.)

Solution 2

Private Sub ADD_Column()

  Dim AddColumn As New DataGridViewCheckBoxColumn

  With AddColumn
    .HeaderText = "ColumnName"
    .Name = "Column Name that will be displayed"
    .Width = 80
  End With

  dgAdmin.Columns.Insert(1, AddColumn)

End Sub

Solution 3

Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dataGridView1.Columns.Insert(0, checkBoxColumn)

Solution 4

I have had this issue once, but I solved it. L load data from dataset and the fill the datagridview. I was setting the readOnly property of the datagridview = True, which means you cant modify the data in the datagridview. Just set the AllowUserToAddColumn to False and make readOnly = False and this will work.

Share:
74,281
Admin
Author by

Admin

Updated on February 22, 2020

Comments

  • Admin
    Admin about 4 years

    I am trying to add a checkbox column to a DataGridView in a simple window forms application.

    I am pulling back some data from a database using ADO.NET, putting into a datatable, and then setting the datagridview datasource to the datatable. I then want to add a checkbox column as the second column. So far I have this code that seems to work:

    ' Code here to connect to database
    Dim da As New SqlDataAdapter(cmd)
    Dim dt As New DataTable
    da.Fill(dt)
    
    MainForm.MyDataGridView.DataSource = dt
    
    Dim ChkBox As New DataGridViewCheckBoxColumn
    
    ChkBox.FlatStyle = FlatStyle.Standard
    MainForm.MyDataGridView.Columns.Insert(1, ChkBox)
    

    This code 'works' and I get MyDataGridView to show the data with the checkbox column in the correct position in the table.

    However, for some reason, I cannot check any of the check boxes in the DataGridView? I have tried lots of things (e.g.altering the readonly state of the column) but cannot get it to work.

    Is there something obvious that I am missing?

  • Admin
    Admin almost 15 years
    Wahid - thanks for taking the time to help. I tried this and I got it working so very grateful for your assistance. Thanks much
  • IsmailS
    IsmailS over 13 years
    I can't select the checkbox. Its always unchecked.
  • Wahid Bitar
    Wahid Bitar over 13 years
    @ Ismail : please be sure that "Read only" is NOT selected