.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

Worksheet.Protect(String, WorksheetProtectionPermissions) Method

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

Namespace: DevExpress.Spreadsheet

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


void Protect(
    string password,
    WorksheetProtectionPermissions permissions
Sub Protect(
    password As String,
    permissions As WorksheetProtectionPermissions


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.


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
worksheet("C3:F8").Borders.SetOutsideBorders(Color.Red, BorderLineStyle.Thin)
worksheet("D5").Value = "Try to change me!"
worksheet("D5").Alignment.Vertical = SpreadsheetVerticalAlignment.Center

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.

See Also