# Protect Cell Range

DsExcel is a Document Solutions product that offers a comprehensive library to create, manipulate, convert, and share Microsoft Excel-compatible spreadsheets.

## Content

DsExcel allows you to protect cells in a worksheet by locking the cells or hiding the formulas in the cells with **Locked** and **FormulaHidden** properties of [IRange](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.html) and [IStyle](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IStyle.html) interfaces.

| **Interface** | **Property** | **Description** |
| --------- | -------- | ----------- |
| @rows=2:IRange | [Locked](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.Locked.html) | Locks specific cell range of a worksheet. |
| [FormulaHidden](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.FormulaHidden.html) | Hides formulas in a specific cell range of a worksheet. |
| @rows=2:IStyle | [Locked](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IStyle.Locked.html) | Locks all worksheet cells with style. |
| [FormulaHidden](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IStyle.FormulaHidden.html) | Hides formulas in all worksheet cells with style. |

> !type=note
> **Note:** By default, Locked property is set to true, and FormulaHidden property is set to false.

> !type=note
> **Note:** Locked and FormulaHidden properties will work when the worksheet is protected. For more information on protecting a worksheet, see [Protect a Worksheet.](/document-solutions/dot-net-excel-api/docs/online/Features/ManageWorksheet/WorkWithSheets#PW)

## Lock Cells

### Lock Specific Cell Range

Refer to the following example code to lock a specific cell range of a worksheet when protected:

```csharp
// Lock cell range C7:C11. 
worksheet.Range["C7:C11"].Locked = true;
```

### Lock Worksheet Cells with Style

Refer to the following example code to lock worksheet cells with style when protected:

```csharp
// Lock cells with custom style.
IStyle style = workbook.Styles.Add("CustomStyle");
style.Locked = true;
worksheet.Range["B2:D18"].Style = style;
```

By default, all the cells in a worksheet have "Normal" style, and you can lock all the worksheet cells by setting [Locked](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IStyle.Locked.html) property of the style of any cell. Refer to the following example code to lock all the worksheet cells:

```csharp
// Lock all worksheet cells.
worksheet.Range["A1"].Style.Locked = true;
```

## Hide Formula in Cells

### Hide Formula in Specific Cell Range

Refer to the following example code to hide formulas in a specific cell range of a worksheet when protected:

```csharp
// Hide formula(s) in cell range C7:C19. 
worksheet.Range["C7:C19"].FormulaHidden = true;
```

### Hide Formula in Worksheet Cells with Style

Refer to the following example code to hide formulas in all worksheet cells with a specific style when protected:

```csharp
// Hide formula in cells with custom style.
IStyle style = workbook.Styles.Add("CustomStyle");
style.FormulaHidden = true;
worksheet.Range["B2:D18"].Style = style;
```

By default, all the cells in a worksheet have "Normal" style, and you can hide formulas in all the worksheet cells by setting [FormulaHidden](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IStyle.FormulaHidden.html) property of the style of any cell. Refer to the following example code to hide the formula in all the worksheet cells:

```csharp
// Hide formula in all worksheet cells.
worksheet.Range["A1"].Style.FormulaHidden = true;
```