Help with adding checkbox column to DataGridView i

2020-06-18 03:27发布

问题:

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?

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



回答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


回答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);
   }

}    


回答4:

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


回答5:

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.



回答6:

Try this:

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