使自定义 DataGridViewCell 只读
我正在制作几个自定义 DataGridViewCell 类来处理 C# 应用程序中的各种情况。自定义类之一与只读数据关联,因此我尝试将单元格本身设置为只读。
我最初尝试在构造函数中设置 ReadOnly 属性,但这样做会导致 InvalidOperationException:“在将单元格添加到行之前无法设置单元格的 ReadOnly 属性。”
我应该重写哪个方法(即哪个方法将单元格添加到行中),以便我可以设置 ReadOnly 属性?
I am making several custom DataGridViewCell classes to handle various cases in my C# application. 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."
Which method should I override (i.e., which method adds the cell to the row), so that I can set the ReadOnly property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来获得所需行为(禁止用户编辑单元格中的数据)的方法是重写 DataGridViewCell 子类中的 EditType 属性:
这可以防止单元格显示编辑控件,从而使单元格读取-仅有的。
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:
This keeps the cell from displaying an edit control, consequently making the cell read-only.