CheckBox Column in DataGridView defaults to ReadOnly being True?

I have a Column in my DGV that is defined as a DataGridViewCheckBoxCell. My query that is bound to the DGV looks like this:

SELECT      0 AS Selected, EquipID, EquipName, Model, SN, Accredited, Description FROM CalEquip ORDER BY TypeID, EquipName

The first column acts as my CheckBox Column in my DGV. My problem is that even when I manually set the ReadOnly property to False, I run the app and when I try to check/uncheck the column, it won't allow me to. When I go back into the designer and look at that Column, it shows the ReadOnly property is True. What am I doing wrong?

Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
Your value of zero is seen as a calculated field, and calculated fields cannot be updated.

Either create a dummy field in you database, or leave out the Selected field and add the Checkbox column through your code after the grid is created, with something like the following:

Dim selectedColumn As New DataGridViewCheckBoxColumn()
selectedColumn.HeaderText = "X"
selectedColumn.DisplayIndex = 0
selectedColumn.Width = 25
BlakeMcKennaAuthor Commented:
Thanks James...that worked!
BlakeMcKennaAuthor Commented:
I am allowed to check/uncheck that field now but now the problem is that it always loads as checked. I need to load in an unchecked mode.
Jacques Bourgeois (James Burger)PresidentCommented:
If you did it through an extra field instead of doing it with the extra column added through code, it gets updated along with the other columns.

You can get rid of that 4 different ways:

- Remove the checks through code before doing an update.
- When finished working with the data, run a SQL command that resets the value to False for all rows where it is True in the database.
- Or run the same command before filling up the grid.
- Do not use the field trick, and use my second recommendation instead.
BlakeMcKennaAuthor Commented:
Thanks James, I'll give it a try!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.