Reputation: 3361
I have a DataGridView
, where I want to disable some cells/rows with setting ReadOnly = true
.
What could be the reason that sometimes this has no effect and the cells/rows are still editable?
Are there other possibilities to prevent editing specific rows or cells? Is it possible to prevent clicking or entering a cell?
Upvotes: 13
Views: 14825
Reputation: 525
Try running a datagridview.Refresh()
after setting the readonly value to true.
Upvotes: 1
Reputation: 73243
One possibility is that if you set the DataGridView's ReadOnly property (in code), then the columns' ReadOnly needs to be reset:
this.dgv.Columns[0].ReadOnly = true;
this.dgv.ReadOnly = false;
// Need to reset the column's ReadOnly state:
this.dgv.Columns[0].ReadOnly = true;
You can also set whole rows as ReadOnly
Upvotes: 0
Reputation: 49978
You could prevent editing with the CellBeginEdit event. If you dont want the cell to be edited, you can cancel the edit. For example, if you only want the first column to be editable, you can do this:
private void dataGridView1_CellBeginEdit(object sender,
DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex != 0)
{
e.Cancel = true;
}
}
Upvotes: 23