简体   繁体   中英

Edit DataGridViewCell on readonly object property

I've bound a DataGridView to a BindingList<MyObj> which has the Value property readonly (protected).

So there I have a method to change its value ( 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?

I tried to set the column.ReadOnly = false , but it throws an exception. It doesn't fire the CellBeginEdit event, so will I have to create another textbox in front of it to do this?

As per MSDN, it would seem the only exception will be:

Exceptions InvalidOperationException There is no owning row when setting this property.

-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.

Also, the Remark section says:

The ReadOnly property indicates whether the data displayed by the cell can be edited. > > 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. 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.

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; it does not affect whether the user > > can delete rows.

Sounds like it's all there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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