简体   繁体   English

在只读 object 属性上编辑 DataGridViewCell

[英]Edit DataGridViewCell on readonly object property

I've bound a DataGridView to a BindingList<MyObj> which has the Value property readonly (protected).我已将 DataGridView 绑定到BindingList<MyObj> ,它具有只读的 Value 属性(受保护)。

So there I have a method to change its value ( MyObj.SetValue(string newValue) ).所以我有一个方法来改变它的值( MyObj.SetValue(string newValue) )。

The standard behavior of DataGridView is also to lock the cell editing, but there's no way to "simulate" an editing and then pass the correct method to update the value? DataGridView 的标准行为也是锁定单元格编辑,但是没有办法“模拟”编辑然后传递正确的方法来更新值?

I tried to set the column.ReadOnly = false , but it throws an exception.我试图设置column.ReadOnly = false ,但它会引发异常。 It doesn't fire the CellBeginEdit event, so will I have to create another textbox in front of it to do this?它不会触发CellBeginEdit事件,所以我必须在它前面创建另一个文本框来执行此操作吗?

As per MSDN, it would seem the only exception will be:根据 MSDN,似乎唯一的例外是:

Exceptions InvalidOperationException There is no owning row when setting this property.异常 InvalidOperationException 设置此属性时没有所属行。

-or- -或者-

The owning row is shared when setting this property.设置此属性时共享所属行。

So, check if the is actually an OwningRow , or if that row happens to be shared.因此,请检查是否实际上是OwningRow ,或者该行是否恰好是共享的。

Also, the Remark section says:此外, Remark section说:

The ReadOnly property indicates whether the data displayed by the cell can be edited. ReadOnly 属性表示单元格显示的数据是否可以编辑。 > > You can set ReadOnly for individual cells, or you can make an entire row or column of > > cells read-only by setting the DataGridViewRow.ReadOnly or DataGridViewColumn.ReadOnly > > properties. > > 您可以为单个单元格设置 ReadOnly,也可以通过设置 DataGridViewRow.ReadOnly 或 DataGridViewColumn.ReadOnly > > 属性将 > > 单元格的整行或整列设为只读。 By default, if a cell's parent row or column is set to read-only, the child > cells will adopt the same value.默认情况下,如果单元格的父行或列设置为只读,则子 > 单元格将采用相同的值。 You can override this default behavior by setting > > > > ReadOnly for individual cells.您可以通过为单个单元格设置 > > > > ReadOnly 来覆盖此默认行为。

You can navigate to a read-only cell, and you can set a read-only cell to be the current > cell.您可以导航到只读单元格,并且可以将只读单元格设置为当前 > 单元格。

ReadOnly only affects whether a cell is editable; ReadOnly 只影响单元格是否可编辑; it does not affect whether the user > > can delete rows.它不影响用户 > > 是否可以删除行。

Sounds like it's all there.听起来一切都在那里。

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

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