Skip to main content
Row

Worksheet.Protect(String, WorksheetProtectionPermissions) Method

Protects worksheet elements to prevent them from being changed by the user.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v23.2.Core.dll

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

void Protect(
    string password,
    WorksheetProtectionPermissions permissions
)

Parameters

Name Type Description
password String

A string that specifies a password for the worksheet. If the password is an empty string, the worksheet can be unprotected without using a password.

permissions WorksheetProtectionPermissions

A WorksheetProtectionPermissions enumeration that allows you to combine enumeration values to specify multiple worksheet elements to protect.

Remarks

If the worksheet is already protected, the Protect method throws an exception. Check the Worksheet.IsProtected property before calling the method.

View Example

Worksheet worksheet = workbook.Worksheets["ProtectionSample"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Protect the worksheet. Prevent end-users from changing worksheet elements.
if (!worksheet.IsProtected)
    worksheet.Protect("password", WorksheetProtectionPermissions.Default);
// Add a note.
worksheet["B2"].Value = "Worksheet is protected with a password. \n You cannot edit or format cells until protection is removed." +
                        "\nTo remove protection, on the Review tab, in the Changes group," +
                        "\nclick \"Unprotect Sheet\" and enter \"password\".";
worksheet.Visible = true;

You can selectively unlock ranges in a protected worksheet. To do this, add a ProtectedRange to the Worksheet.ProtectedRanges collection. You can specify a password to edit the protected range with the ProtectedRange.SetPassword method and give specific users permission to edit the range with the ProtectedRange.CreateSecurityDescriptor method.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Protect(String, WorksheetProtectionPermissions) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also