Sur*_*tha 7 c# excel vsto ms-office
Range range= (Range)this.workSheet.Cells[1,1];
range.AllowEdit = false;
Run Code Online (Sandbox Code Playgroud)
当我将AllowEdit属性设置为false时,将显示编译错误:
错误:无法将属性或索引器"Microsoft.Office.Interop.Excel.Range.AllowEdit"分配给 - 它是只读的
如何将单元格范围设置为只读?
当我使用此范围的验证时,我的CellContentChanged事件有一些异常.
这是CellContentChanged中的代码:
var updater = new Action<StyleColorItem>(
item =>
{
var editedItem = _taskViewModel.TrackedItems.First(it => it.Id == item.Id);
// Above line I am getting the exception like "Sequence contains no matching element"
editedItem.Update(item);'
});
Run Code Online (Sandbox Code Playgroud)
Excel 中无法将单元格设置为只读。
您可以在 C# 代码中做的是,定义一个变量或列表中的“只读”单元格,订阅 SheetChange 事件,在 SheetChange 事件中,如果该单元格发生更改,只需撤消该操作改变。
示例 private List readOnlyCells = new List();
private void OnActiveSheetCellChange(object changedSheet, Excel.Range changedCell)
{
if (readOnlyCells.Contains(changedCell))
changedCell.Value = string.Empty;
//.... YOUR CODE
Run Code Online (Sandbox Code Playgroud)
更新
另一种选择是使用数据验证:
changedCell.Validation.Add(Excel.XlDVType.xlValidateCustom, Type.Missing, Type.Missing, "\"\"");
Run Code Online (Sandbox Code Playgroud)
使用它你将拥有更少的控制权并且单元将根本不接受任何输入。