Checkboxes in first row of DataGridView are readonly

I have a datagridview in my app which is bound to a datatable, the datagridview has predefined columns, both text and checkboxes.

For some reason the top row will not allow me to change the state of the checkboxes by clicking on them. I can edit the text in the row - so it is not a problem of the row being readonly. Also, if I sort the data by clicking a header then whichever row ends up at the top will not accept changes to the checkboxes and the previously uneditable row (which is now further down the list) now does accept changes to the checkboxes.

It seems to me that there is a problem with the DataGridView control in the framework, so my question is: has anyone else come across this and did they find a workaround?
LVL 15
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try putting this on form load event.

Datagridview.column(0).readonly = true
never faced this kind of problem. it has something to do with the code only.
if you try to do a simillar thing on a new form is it behaving in the same way?
can you post your existing code, specially from the places where you are creating the columns and binding the data.

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

I think you had a similar problem with this guy, do check it out.
Solar_FlareAuthor Commented:
OK I have managed to figure out what was causing it, heres an explanation:

I want the checkbox columns to be as small as possible for displaying item statuses, so I set the DGV to autosize columns = Fill. Then on form load I loop through all the columns and assign images to the header cells as they are smaller than text. I also set the column sizing in the loop:
For Each c As DataGridViewColumn In dg1.Columns
            If c.DataPropertyName.StartsWith("Status") Then
                    Dim hc As New DataGridViewImageColumnHeaderCell
                    hc.Image = ImageList1.Images(c.Index)
                    c.HeaderCell = hc
                Catch ex As Exception
                End Try

                c.MinimumWidth = 18
                c.FillWeight = 1
                c.HeaderText = ""
      end if

Not that I set the FillWieght to 1 in order to minimize the space that the columns occupies - instead I rely on the minimum width attribute to ensure the checkbox is visible.

if I change the FillWeight to something larger, say 20, then it all works correctly! I can only presume that there is some very obscure bug in the datagridview in situations where the fillweight is tiny (perhaps less than 0.5% so gets rounded to 0?)which affects the click handling for checkbox columns in the top row.

I tried replicating the problem in a new project but the original DGV with the problem is quite complex and I don't have time to copy it exactly. A quick attempt did not yield the same behaviour so this is obviously pretty obscure.
Solar_FlareAuthor Commented:
appari, you were correct that it had to do with my code. I will accept an answer so that someone else with this problem can find it.
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.