简体   繁体   English

使自定义DataGridViewCell只读

[英]Making Custom DataGridViewCell ReadOnly

I am making several custom DataGridViewCell classes to handle various cases in my C# application. 我正在制作几个自定义DataGridViewCell类,以处理C#应用程序中的各种情况。 One of the custom classes is associated with read-only data, so I'm attempting to make the cell itself read-only. 自定义类之一与只读数据相关联,因此我试图将单元格本身设置为只读。

I initially tried setting the ReadOnly property in the constructor, but doing so causes an InvalidOperationException: "ReadOnly property of a cell cannot be set before it is added to a row." 我最初尝试在构造函数中设置ReadOnly属性,但这样做会导致InvalidOperationException:“在将单元格的ReadOnly属性添加到行之前,无法对其进行设置。”

Which method should I override (ie, which method adds the cell to the row), so that I can set the ReadOnly property? 我应该重写哪个方法(即哪个方法将单元格添加到行中),以便可以设置ReadOnly属性?

It looks like the way to get the desired behavior (prohibiting the user from editing the data in the cell) is to override the EditType property in the DataGridViewCell sub-class: 看来,获得所需行为(禁止用户编辑单元格中的数据)的方法是重写DataGridViewCell子类中的EditType属性:

    public override Type EditType
    {
        get
        {
            return null;
        }
    }

This keeps the cell from displaying an edit control, consequently making the cell read-only. 这可以防止单元格显示编辑控件,从而使该单元格为只读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM