Cells in a worksheet have a Locked property to get or set if the cell is marked as locked from editing. To programmatically lock only specific cells or ranges, we must first unlock all cells in the worksheet by setting their Locked property to false. We must do this because, by default, all of the cells are locked. Then, with all of the cells unlocked, we can set the Locked property to true for only the cell or range of cells we require to be un-editable.
In this code, we are unlocking all of the worksheet's cells by setting a column object’s Locked property to false, then using a cell object’s Locked property to lock the cell range B2:D5.
C#
FpSpread fpSpread1 = new FpSpread(); // Initialize Spread.NET
fpSpread1.LegacyBehaviors = LegacyBehaviors.None;
fpSpread1.TabStrip.Editable = true;
// Set style for locked cells
fpSpread1.ActiveSheet.LockBackColor = System.Drawing.Color.LightCyan;
fpSpread1.ActiveSheet.LockForeColor = System.Drawing.Color.Green;
// Using the Column Object unlock all the cells
FarPoint.Win.Spread.Column columnobj; // Column Object
int count = fpSpread1.ActiveSheet.Columns.Count - 1; // Get the column count
columnobj = fpSpread1.ActiveSheet.Columns[0, count]; // Get all of the columns in the worksheet
columnobj.Locked = false; // Unlock the cells
// Use the Cells Object to lock a specific cells or cell ranges
FarPoint.Win.Spread.Cell cellobj; // Cell Object
cellobj = fpSpread1.ActiveSheet.Cells["B2:D5"]; // Get a cell range
cellobj.Value = "Locked Cells"; // Display Text
cellobj.Locked = true; // Lock the cell range
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.All); // Protect the worksheet
VB.NET
Dim fpSpread1 As FpSpread = New FpSpread() 'Initialize Spread.NET
fpSpread1.LegacyBehaviors = LegacyBehaviors.None
fpSpread1.TabStrip.Editable = True
'Set style for locked cells
fpSpread1.ActiveSheet.LockBackColor = System.Drawing.Color.LightCyan
fpSpread1.ActiveSheet.LockForeColor = System.Drawing.Color.Green
'Using the Column Object Unlock all the cells
Dim columnobj As FarPoint.Win.Spread.Column 'Column Object
Dim count As Integer = fpSpread1.ActiveSheet.Columns.Count - 1 'Get the column count
columnobj = fpSpread1.ActiveSheet.Columns(0, count) 'Get all of the columns in the worksheet
columnobj.Locked = False 'Unlock the cells
'Use the Cells Object to lock a specific cells or cell ranges
Dim cellobj As FarPoint.Win.Spread.Cell 'Cell Object
cellobj = fpSpread1.ActiveSheet.Cells("B2:D5") 'Get a cell range
cellobj.Value = "Locked Cells" 'Lock the cell range
cellobj.Locked = True 'Lock the cell range
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.All) 'Protect the worksheet