Row locking

Received : 2007/08/14 15:45:56
Message : I'd like to know if there is a way to lock an entire row from being edited in Aspose.Grid.Desktop.

If so, could someone send me the c# code for doing so?


This message was posted using Aspose.Live 2 Forum
Hi,
Thanks for considering Aspose.
Yes, you may do it. May the following code helps you for your need as it protects the first row only.
Explaination: In the code, first we will unlock all the cells in the sheet. Then, we lock the cells in the first row only and protect the sheet.
APIs used: Use Style object with StyleFlag struct (the struct is used to apply your desired formattings only). And use Row.ApplyStyle method for the task. Finally use Protection class to protect the sheet.
Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];
Style style;
StyleFlag flag;
for(int i = 0; i <= 255; i ++)
{
style = sheet.Cells.Columns[(byte)i].Style;
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}

style = sheet.Cells.Rows[0].Style;
style.IsLocked = true;

flag = new StyleFlag();
flag.Locked = true;

sheet.Cells.Rows[0].ApplyStyle(style, flag);

Aspose.Cells.Protection protection = sheet.Protection;

wb.Save("d:\\test\\lockedrowcells.xls", FileFormatType.ExcelXP);
Thank you.

Hi,

Sorry, I thought you are using Aspose.Cells. But actually you are using Aspose.Grid.Web. So, kindly ignore my previous post and consider this one.

Well, you may use WebCell.IsReadOnly and WebCell.IsLocked properties in a loop to lock a complete row for your need.

E.g.,

//Lock the second row in the first worksheet.

for(int i = 0; i<=GridWeb1.WebWorksheets[0].Cells.MaxColumn;i++)

{

GridWeb1.WebWorksheets[0].Cells[1,i].IsReadonly = true;

}

Thank you.