DataGridViewComboBoxCell.ReadOnly = true, but can still change the selected value

1.6k views Asked by At

I am strugling with a problem with DataGridView and DataGridViewComboBoxCell in C# (Visual Studio 2013, .NET 4.5.1)

The DataGridView has 3 columns with DataGridViewComboBoxColumn and 3 columns with DataGridViewTextBoxColumn.

The DataGrid is bound to a DataTable

I need to set the readonly property individually for each cell:

For existing rows: Only the last cell (DataGridViewTextBoxCell) shall be editable.

When adding new row, all cells except two textbox cells shall be editable.

My problem is that the end user can still change the selection of the DataGridViewComboBoxCells even when ReadOnly = true;

Setting ReadOnly=true on DataGridViewTextBoxCells works fine

Setting ReadOnly=true on DataGridViewComboBoxCell has no effect. When writing out the ReadOnly property, it returns true, but the cell is still editable.

2

There are 2 answers

0
tezzo On BEST ANSWER

You can use a similar code in your DataGridView.CellBeginEdit:

If Not <your code to verify if you are adding a new row> Then
    If Not YourDataGridView.Columns(e.ColumnIndex).Name = "EditableColumnName" Then
        e.Cancel = True
    End If
End If
0
Alvaro Rodriguez Scelza On

This should correct the readonly=true ignored problem in a more direct way: https://stackoverflow.com/a/48471383/5750078