Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting cell.ReadOnly has no effect

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?

like image 606
Elmex Avatar asked Mar 15 '11 14:03

Elmex


2 Answers

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;
   }
}
like image 64
SwDevMan81 Avatar answered Nov 15 '22 05:11

SwDevMan81


Try running a datagridview.Refresh() after setting the readonly value to true.

like image 33
Ryan Lutz Avatar answered Nov 15 '22 06:11

Ryan Lutz