Spread Windows Forms 18 Product Documentation / Developer's Guide / Sheets / Protecting a Worksheet
Protecting a Worksheet

You can protect a worksheet with a password in order to prevent other users from accidentally or deliberately changing, moving, or deleting data. In Spread for WinForms, the Protect method provides a variety of password protection and lock options that are available in the Protect Sheet dialog box. 

Using Code

You can choose to protect or unprotect a worksheet using the Protect method which passes the WorksheetLocks enumeration as the parameter.

fpSpread1.LegacyBehaviors = LegacyBehaviors.None;
activeSheet.Cells["A1:A4"].Value = 1;
activeSheet.Cells["B1:B4"].Value = 3;
activeSheet.Cells["C1:C4"].Value = 5;
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.DeleteColumns | WorksheetLocks.DeleteRows);

You can also set various types of protection options available for a worksheet using the IProtection interface. Each property from this interface gets a Boolean value, true or false indicating whether a particular action is allowed on a protected worksheet. For example, you can set the AllowDeletingColumns property as shown below: 

fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.DeleteColumns);
GrapeCity.Spreadsheet.IProtection protection = fpSpread1.AsWorkbook().ActiveSheet.Protection;
var value = protection.AllowDeletingColumns;
MessageBox.Show(value.ToString());

In the above example, the MessageBox will show a false value as the worksheet has already been locked and protected to prevent column deletion.

Using Runtime UI

You can implement the Protect method in the runtime UI by setting the tab strip as editable. This allows you to open the Protect Sheet dialog.

fpSpread1.TabStrip.Editable = true;

The following GIF illustrates opening protect sheet dialog box at runtime.

Using Spread Designer

To access the Protect Sheet dialog, navigate to the Protect Sheet button in the Protect group under the Review tab of the Spread ribbon.

Steps to activate or deactivate protect sheets options

  1. Select the multiple options you want to implement while protecting the worksheet from the Protect Sheet dialog box. Enter the password to protect the worksheet.   

    protectws-dialogbox

  2. When you enter password to protect the worksheet, another dialog appears to confirm the password.

    protectws-password

  3. If the caps lock is on, a label indicator "CAPS LOCK ON" appears on the left side of the dialog box.

    protectWS-capslock

Once a password is successfully set, you can access the protected worksheet, but cannot make any changes to the sheet.  To unprotect the worksheet enter the set password using the Unprotect Sheet dialog.

protectws-unprotect 

Limitations

Currently worksheet protection options do not include protection of PivotTable, PivotChart, and Edit Scenarios, but still support import/export for those options. The worksheet protection options work only when LegacyBehaviors.Protect is not set, otherwise the old behavior is applied.