views:

25

answers:

1

This is either me not understanding the order of constructor execution or not understanding the precedence of ReadOnly fields on DataGridViews.

class Form1 : Form
{
    public Form1()
    {
        DataGridView gv = new DataGridView();
        Controls.Add(gv);
        gv.Columns.Add("foo","foo");
        gv.Rows[gv.Rows.Add()].ReadOnly = true;
        gv[0,0] = new DerivedCell();
        //gv[0,0].ReadOnly = false;
    }
}

class DerivedCell : DataGridViewTextBoxCell
{
    public DerivedCell()
    {
        ReadOnly = false;
    }
}

The commented line is needed if I want to make the cell editable, but I don't understand why that isn't taken care of in the DerivedCell ctor.

A: 

If you instead do this

DataGridViewTextBoxCell foo = new DerivedCell();
gv[0, 0] = foo;

you will see that foo.ReadOnly is false after the first line but true after the second line. So it is the indexer of DataGridView that does this (sets the ReadOnly property of the new cell to whatever value the old cell had). Don't ask me why though.

D.H.