Skip to main content
A newer version of this page is available. .
Row

Worksheet.Protect(String, WorksheetProtectionPermissions) Method

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

Namespace: DevExpress.Spreadsheet

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

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.

Dim worksheet As Worksheet = workbook.Worksheets(0)

' Protect a worksheet. Prevent users from making changes to worksheet elements.
If Not worksheet.IsProtected Then
    worksheet.Protect("password", WorksheetProtectionPermissions.Default)
End If
workbook.BeginUpdate()
worksheet("C3:F8").Borders.SetOutsideBorders(Color.Red, BorderLineStyle.Thin)
worksheet("D5:E6").Merge()
worksheet("D5").Value = "Try to change me!"
worksheet("D5").Alignment.Vertical = SpreadsheetVerticalAlignment.Center
workbook.EndUpdate()

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