7

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?

6 Answers 6

15

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

Wahid - thanks for taking the time to help. I tried this and I got it working so very grateful for your assistance. Thanks much
I can't select the checkbox. Its always unchecked.
@ Ismail : please be sure that "Read only" is NOT selected
7
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

Comments

3
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
   if(dataGridView1.Columns.Count == 13 )
   {
       DataGridViewCheckBoxColumn chkSelect = new DataGridViewCheckBoxColumn();
       {
           chkSelect.HeaderText = "Select All";
           chkSelect.Name = "chkSelect";
           chkSelect.Selected = false;


       }
       dataGridView1.Columns.Insert(13, chkSelect);
   }

}    

Comments

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

Comments

1

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.

Comments

0

Try this:

        DataGridViewCheckBoxColumn chkBoxCol = new DataGridViewCheckBoxColumn();
        DataGridView1.Columns.Add(chkBoxCol);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.