Locking columns at a time doesn't have any effect


#1

Hi

I was trying to lock a whole column at a time, but the style doesn't seem to be set when I use the following code:


//ws is the active worksheet

ws.Cells[Convert.ToByte(column)].Style.IsLocked = true;

When I check the worksheet afterwards, the columns are not locked at all. There are no other style manipulations on the cells/columns after this piece of code. Other style manipulations like


ws.Cells.Columns[Convert.ToByte(column)].Style.Custom = ApplicationSettings.AppDatePattern;

are performed on other columns and they do have affect.

Any help is welcome.

Kind regards
Frederick


#2

Dear Frederick,

In default state, cell is locked. The following is my sample code to lock a column which is not locked in template file.

Excel excel = new Excel();
excel.Open("d:\\test\\lockcolumn.xls");
excel.Worksheets[0].Cells.Columns[0].Style.IsLocked = true;
excel.Save("d:\\test\\abc.xls");

And which version of Aspose.Excel are you using?


#3

The same problem occurs when trying to manipulate rows.


for (int i=0;i <= titleRow;i++)
{
ws.Cells.RowsIdea [I].Style.IsLocked = true;
}

this doesn't work.

Although when I write an iteration to handle cell by cell then their is no problem. It seems that Style isn't set when applying it to a collection like ws.Cells.Rows or ws.Cells.Columns


#4

You can also try this:

for (int i=0;i <= titleRow;i++)
{

Style style = ws.Cells.Rows[ i ].Style;

style.IsLocked = true;

ws.Cells.Rows[ i ].Style = style;
}


#5
Laurence wrote:

You can also try this:

for (int i=0;i <= titleRow;i++)
{

Style style = ws.Cells.Rows[ i ].Style;

style.IsLocked = true;

ws.Cells.Rows[ i ].Style = style;
}

Isn't that dangerous? I mean, wouldn't I lose other particular style settings if I set the Style using a new Style object because I think the default values for the other style properties will get applied.

The version I uses is 3.4.0.0


#6

No.

You don't use a new style object. My sample code changes the style object and re-assign to row or column. When the style is changed, it internally changes style for each cells. Other style settings will not be lost.