Protect Cells

Protecting Cells

Aspose.Cells.GridWeb provides a few different techniques for controlling the protection level on cells when the control is in Edit mode (the default mode). This protects cells from being modified by end users.

Making All Cells Read Only

To set all cells in a worksheet to read only, call the worksheet’s SetAllCellsReadonly method.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Setting all cells of the worksheet to Readonly
sheet.SetAllCellsReadonly();

Making All Cells Editable

To remove protection from all cells, call the worksheet’s SetAllCellsEditable method. This method has the opposite effect to the SetAllCellsReadonly method.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Setting all cells of the worksheet to Editable
sheet.SetAllCellsEditable();

Making Selected Cells Read Only

To protect only a range of cells:

  1. First make all cells editable by calling the SetAllCellsEditable method.
  2. Specify the range of cells that to protect by calling the worksheet’s SetReadonlyRange method. This method takes the number of rows and columns to specify the range of cells.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Setting all cells of the worksheet to Editable first
sheet.SetAllCellsEditable();
// Finally, Setting selected cells of the worksheet to Readonly
sheet.SetReadonlyRange(3, 2, 4, 1);

Making Selected Cells Editable

To un-protect a range of cells:

  1. Make all cells read only by calling the SetAllCellsReadonly method.
  2. Specify the range of cells that to be editable by calling the worksheet’s SetEditableRange method. This method takes the number of rows and columns to specify the range of cells.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Setting all cells of the worksheet to Readonly first
sheet.SetAllCellsReadonly();
// Finally, Setting selected cells of the worksheet to Editable
sheet.SetEditableRange(3, 2, 4, 1);