OK, so I have this piece of code where I try to set all rows but a few to readonly.
Since the condition change on a per-row basis I set all cells to ReadOnly and then unlock those I need.
There's no databinding involved. I use the grid "as is".
cells[1] is a DataGridViewTextBoxCell
cells[4] is a DataGridViewComboBoxCell
When I do this:
row.ReadOnly = true;
row.Cells[1].ReadOnly = false; //Successfully changes from true to false.
row.Cells[4].ReadOnly = false; // This DOES NOT work!
OK, so I tried something else:
foreach (DataGridViewCell cell in row.Cells)
{
cell.ReadOnly = true; //Successfully changes from false to true.
}
row.Cells[1].ReadOnly = false; // This DOES NOT work!
row.Cells[4].ReadOnly = false; // This DOES NOT work!
Here's the column definition:
textBoxColumn = new DataGridViewTextBoxColumn();
textBoxColumn.Name = "Column0";
textBoxColumn.HeaderText = "My column header";
textBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
textextBoxColumn.Resizable = DataGridViewTriState.True;
textextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView.Columns.Add(textBoxColumn);
comboBoxBoxColumn = new DataGridViewComboBoxColumn();
comboBoxBoxColumn.Name = "Column4";
comboBoxBoxColumn.HeaderText = "My column header";
comboBoxBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
comboBoxBoxColumn.Resizable = DataGridViewTriState.True;
comboBoxBoxColumn.Items.Add("Item 1");
comboBoxBoxColumn.Items.Add("Item 2");
comboBoxBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView.Columns.Add(comboBoxBoxColumn);
I just don't get it.
Edit:
This is how I create the row: I first create all the (hundreds of) rows and then I add them to the DataGridView in one swoop, to avoid UI refreshs:
row = new DataGridViewRow();
for (Int32 i = 0; i < dataGridView.Columns.Count; i++)
{
DataGridViewCell cell;
DataGridViewColumn column;
column = dataGridView.Columns[i];
cell = (DataGridViewCell)(column.CellTemplate.Clone());
row.Cells.Add(cell);
}