Alex Alex - 6 months ago 28 Question

Unable to make DataGridView Columns readonly

I am making an application where the user can enter and exit and "edit" state on different items. When entering the edit state, I want to enable certain columns on a DataGridView and when they exit, disable them. The code below is run when the Boolean


'Change ReadOnly to Not EditMode
'dgv.ReadOnly = Not EditMode 'Works
dgv.Columns("colCode").ReadOnly = Not EditMode 'Does not work
dgv.Columns("colText").ReadOnly = Not EditMode 'Does not work
dgv.Columns("colTarget").ReadOnly = Not EditMode 'Does not work
dgv.Columns("colCheck").ReadOnly = Not EditMode 'Does not work

When changing the entire DataGridView ReadOnly property, the grid becomes editable/not editable like I would expect it too, but I only want to enable 4/6 columns. The column names are correct, and the logic is the same, but enabling the columns individually is not changing the ReadOnly property and I am not able to edit the columns. Stepping through the debugger, when entering edit mode I can see
dgv.Columns("colCode").ReadOnly = Not EditMode
evaluate to
dgv.Columns("colCode").ReadOnly = False
but stepping past, the ReadOnly property remains true...


If dgv.ReadOnly = true then the columns are all forced to be ReadOnly = True. So set dgv.ReadOnly = False and set the ReadOnly property only on the columns to true.