Add a Checkbox inside a DataGridView Datacell

IrvineCAGuy

New member
Joined
Nov 30, 2010
Messages
4
Programming Experience
10+
I am creating a schedule calendar where the header columns are set to show the next 7 days as | User name | Mon | Tue | Wed| Thu| Fri | Sat | and the first row is...
| |11/20|11/30| etc.

Then I want to set the "dates" cells with a checkbox in each. My code is:

For iDriverLoop = 0 To .Rows.Count - 1
oDataRow = New DataGridViewRow
oDataCell = New DataGridViewTextBoxCell
oDataCell.Tag = (.Rows(iDriverLoop).Item("iDriverID"))
oDataCell.Value = (.Rows(iDriverLoop).Item("sDriverCode"))
oDataRow.Cells.Add(oDataCell)

oDataCell = New DataGridViewTextBoxCell
oDataCell.Value = (.Rows(iDriverLoop).Item("sDriverName")
oDataRow.Cells.Add(oDataCell)

For iDayLoop = 0 To iNumberOfDays
oDataCell = New DataGridViewCheckBoxCell
oDataRow.Cells.Add(oDataCell)
Next
' Add in this row
Me.DGSchedule.Rows.Add(oDataRow)
Next

The code runs, but when the control render I et "System.FormatException: Formatted value of the cell has the wrong type.

Do I have to create a CheckBoxColumn type - then I won't be able to display text in the first datarow in the 'checkbox' columns?
 
Is there a reason you're not just adding a DataGridViewTextBoxColumn and a DataGridViewCheckBoxColumn to your grid in the designer? If you do that then you simply add a row to the grid and the cells are created for you.
 
I am creating the DataGridView programmatically. The checkboxcolum actually has non-checkbox text in the first datarow.
The datagrid is not databound.
I've decided to just use a Wingdings checkmark symbol as text, then toggle it from blank to checkmark.
But I would still like to figure out how to mix types in a column, if possible.

Thanks
 
If it's only the first row that doesn't have a check box then you'd be better off using a check box column, whether you create it in the designer or in code. You can then simply create a DataGridViewTextBoxCell and assign it to the first cell in that column. To do that, simply assign to the grid's Item property, specifying the row and column index.
 
Back
Top