We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Editing Multiple Cells of Spreadsheet which are protected.


I am developing a spreadsheet application. In the application, spreadsheet control loads Excel file using the following lines of code in the loading event.


Loading event works fine.

Also the application doesn't allow a user to edit any cells. So I added a code like this (I had to loop through worksheets since the workbook has many worksheets):
       private void Spreadsheet1_WorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
            string keyName = string.Empty;
            for (int i = 0; i < spreadsheet1.Workbook.Worksheets.Count; i++)
                spreadsheet1.ProtectSheet(spreadsheet1.Workbook.Worksheets[i], "", ExcelSheetProtection.All);
                keyName = spreadsheet1.Workbook.Worksheets[i].Name;
                spreadsheet1.GridCollection[keyName].AllowEditing = false;

But when I edit the cell values in the run-time, sometimes exception occurs saying object reference not set.
The code for editing is as following:
          for (int i = 0; i < 15; i++)
                this.spreadsheet1.Workbook.Worksheets["Sheet3"].Range[i + 1, 1].Value2 = i;

I appreciate any opinion or suggestion.

1 Reply

KB Kanimozhi Bharathi Syncfusion Team January 30, 2017 11:34 AM UTC

Hi Hayoung Kim,   
If you protect the sheet in Spreadsheet, then by default the users will not be able to edit the cells like MS Excel behavior. So please find the below code example for your reference,   
for (int i = 0; i < spreadsheet.Workbook.Worksheets.Count; i++)   
   spreadsheet.ProtectSheet(spreadsheet.Workbook.Worksheets[i], "", ExcelSheetProtection.All);   
Please find the UG link for your reference,   
If the mentioned “Sheet3” is not present in the workbook, then the exception “object reference not set” will occur. Also, if you want to set the cell value at runtime in spreadsheet, use SetCellValue method like below code example   
for (int i = 0; i < 15; i++)   
  var range = spreadsheet.Workbook.Worksheets["Sheet3"].Range[i + 1, 1];   
  spreadsheet.ActiveGrid.SetCellValue(range, i.ToString());   
  spreadsheet.ActiveGrid.InvalidateCell(i+1, 1);   
Please find the UG link for your reference,   
Please find the sample link for your reference,   
Kanimozhi B   

Live Chat Icon For mobile
Up arrow icon