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?
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?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2003118&SiteID=1
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.StartsW ith("Statu s") Then
Try
Dim hc As New DataGridViewImageColumnHea derCell
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.
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.StartsW
Try
Dim hc As New DataGridViewImageColumnHea
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.
ASKER
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.
Datagridview.column(0).rea