views:

2728

answers:

2

Hi

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?

+3  A: 

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.

Wahid Bitar
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
@ Ismail : please be sure that "Read only" is NOT selected
Wahid Bitar
+1  A: 
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);
   }

}    
swati