开发者

Setting the ReadOnly property on derived DataGridViewCells

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()
    {
开发者_如何学JAVA        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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜