Programmatically changing ReadOnly mode in rows of a DataGridView

Without explaining the whole context, my problem is basically this:

I have a datagridview on a Windows Form that is associated with the Entity Framework DbSet: dbSet<TEntity>.Local.ToBindingList() .

If I set the datagridview ReadOnly property to true (in design mode), then this statement in my code:

  myDataGridView.Rows[rowIndex].ReadOnly = false; 

It works without changing the value! (And no, my data source is not just readable.)

Passing through the cells in a row and setting each ReadOnly property of each cell also does not work:

  foreach (DataGridViewCell cell in myDataGridView.Rows[rowIndex].Cells) { cell.ReadOnly = false; } 

.ReadOnly - Gets or sets a value indicating whether the user can edit the cells of the DataGridView control. For some reason he cannot install .

Is there a reason this will happen? I thought it might be that the datagridview ReadOnly property will override any changes made to specific rows / cells (i.e. the whole form should be either readonly or not), but apparently this worked for someone ...

There are other ways to achieve the ultimate goal, but I would like to know why I cannot change this property, as this would be the easiest solution for my situation.

<h / "> EDIT:

Let me clarify my question:

Again, I know that there are other ways to achieve my ultimate goal, but I would like to answer this question:

Sorry for the primitive example, but to easily replicate the problem, create a WinForm application, drop a datagridview on it and skip this code into your project:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dataGridView1.ReadOnly = true; string[] row1 = new string[] { "test", "test1" }; string[] row2 = new string[] { "test2", "test3" }; string[] row3 = new string[] { "test4", "test5" }; dataGridView1.Columns.Add("1", "1"); dataGridView1.Columns.Add("2", "2"); dataGridView1.Rows.Add(row1); dataGridView1.Rows.Add(row2); dataGridView1.Rows.Add(row3); dataGridView1.Rows[1].ReadOnly = false; } } 

If you set a breakpoint on the last line and go through it, you will see that the ReadOnly property DOES NOT CHANGE! Why??

+6
source share
1 answer

So, I found a solution / workaround for my question, although I'm still not sure about the reasons WHY ...

Apparently, if you want to change the ReadOnly property of the row after row of the DataGridView, you cannot set the main property of DataGridView.ReadOnly, since obviously this will undo any subsequent changes.

To reuse my previous example, a workaround would be to loop through the rows and set each ReadOnly ROW property (as opposed to setting the ReadOnly datagridview property), THEN you can change each ReadOnly property individually:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //dataGridView1.ReadOnly = true; string[] row1 = new string[] { "test", "test1" }; string[] row2 = new string[] { "test2", "test3" }; string[] row3 = new string[] { "test4", "test5" }; dataGridView1.Columns.Add("1", "1"); dataGridView1.Columns.Add("2", "2"); dataGridView1.Rows.Add(row1); dataGridView1.Rows.Add(row2); dataGridView1.Rows.Add(row3); foreach (DataGridViewRow row in dataGridView1.Rows) { row.ReadOnly = true; } dataGridView1.Rows[1].ReadOnly = false; } } 

This works fine for me, however any understanding of why the code in my original question is not working correctly would be appreciated!

+4
source

All Articles