Link to home
Start Free TrialLog in
Avatar of Solar_Flare
Solar_Flare

asked on

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?
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Try putting this on form load event.

Datagridview.column(0).readonly = true
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you had a similar problem with this guy, do check it out.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2003118&SiteID=1
Avatar of Solar_Flare
Solar_Flare

ASKER

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
                Try
                    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
next

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.

BUT...
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.
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.