Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Changing ReadOnly Mode on DataGridView Rows

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

I have a datagridview on a Windows Form which is bound to an Entity Framework DbSet: dbSet<TEntity>.Local.ToBindingList().

If I set the datagridview's ReadOnly property to true (in design view), and then have this statement in my code:

  myDataGridView.Rows[rowIndex].ReadOnly = false;

It steps right through without changing the value! (And no, my datasource is not readonly.)

Looping through the cells in the row and setting each cell's ReadOnly property individually doesn't work either:

  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. But it's acting like it can't set for some reason.

Is there a reason that this would happen? I was thinking that maybe the datagridview's ReadOnly property would override any changes made to specific rows/cells (ie. the whole form has to be either readonly or not), but apparently this worked for someone...

There are other ways to accomplish my end-goal, but I would like to know why I can't change this property, as this would be the simplest solution for my situation.


EDIT:

Let me try to clarify my question:

Again, I am aware that there are other ways to accomplish my end-goal, but would like an answer to this issue:

Sorry for the primitive example, but to easily replicate the issue, create a WinForm application, throw a datagridview on it, and past 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 put a breakpoint on the last line, and step through it, you will see that the ReadOnly property DOES NOT CHANGE! Why??

like image 752
sǝɯɐſ Avatar asked Dec 21 '12 19:12

sǝɯɐſ


1 Answers

So I have found a solution/workaround for my question, althought I am still unsure of the reasons WHY...

Apparently, if you want to change the ReadOnly property of a DataGridView row by row, you cannot set the main DataGridView.ReadOnly property, as evidently this overrides any subsequent changes.

To reuse my previous example, the workaround would be to loop through the rows and set each ROW's ReadOnly property (as opposed to setting the datagridview's ReadOnly property), THEN you can change each row's 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 is working great for me, however any insight as to why the code in my original question does not work correctly would be appreciated!

like image 125
sǝɯɐſ Avatar answered Oct 22 '22 20:10

sǝɯɐſ