We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Gridweb - SetStyle throws exception: Collection was modified; enumeration operation may not execute

I just upgraded Aspose.Cells.Gridweb dll and now the WebWorkSheet.Cells.SetStyle() method is failing. It worked fine in the previous version.

I am using Aspose.Cells.GridWeb version

Previously I was using Aspose.Cells.GridWeb version

This is the code that was working and now fails:

protected void Page_Load(object sender, EventArgs e)


if (!Page.IsPostBack == true)


License license = new License();


String connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program Files\\Aspose\\Aspose.Cells for .NET\\Demos\\Common\\Database\\demos.mdb";

OleDbConnection conn = new OleDbConnection(connString);

//OleDbCommand cmd = new OleDbCommand("SELECT *, +1 FROM PRODUCTS", conn);

OleDbCommand cmd = new OleDbCommand("SELECT *, -1 FROM PRODUCTS", conn); //<-- this was crashing with previous dll

OleDbDataAdapter da = new OleDbDataAdapter(cmd);

DataSet ds = new DataSet();


da.Fill(ds, "PRODUCTS");


WebWorksheet ws = gridWeb1.WebWorksheets[0];


ws.EnableCreateBindColumnHeader = true;

ws.DataSource = ds.Tables[0];



Aspose.Cells.GridWeb.TableItemStyle style;

foreach (WebCell cell in ws.Cells)


style = cell.GetStyle();

style.ForeColor = Color.Red;

ws.Cells.SetStyle(cell.Row, cell.Column, 1, 1, style);




I tried attaching a zip of the project that works with old dlls and the project that now fails with new dlls. But the upload kept timing out.

Full error information:

System.InvalidOperationException was unhandled by user code
Message=Collection was modified; enumeration operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
at dynamicAsposeGridWeb.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Dev\_Sandbox\dynamicAsposeGridWeb-SetStyle-newdlls\WebForm1.aspx.cs:line 42
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The error doesn't occur at the .SetStyle call. That works. The error occurs when it goes back to loop to the next cell, the ForEach statement.

Still trying to upload zipped examples.


We are sorry for your issue.

Well, we need your sample runnable project to reproduce the issue on our end. Also, there should be no problem with attaching attachments in the forums, could you try it now if it works fine. If you still find the issue with uploading attachments, please use some free drives e.g Skydrive to upload your sample project (you may zip prior attaching the project), also attach your template files if you have any. We will check your issue soon.

Also, we recommend you to kindly use our latest Aspose.Cells.GridWeb v2.7.18.xxxx version, you may get that version after installing our latest official release Aspose.Cells for .NET v7.4.3:

Thank you.

two projects attached. one with old dll which works, one with new dll which doesn’t work.

Just downloaded Aspose.Cells 7.4.3 and re-tested with Aspose.Cells.GridWeb dll version and the problem is still there.


Thanks for sharing the sample projects.

After an initial test, I can notice the issue as you have mentioned with latest version v2.7.x.xxxx. I got the exception: “Collection was modified; enumeration operation may not execute” on the last few lines regarding get/set styles.

I have logged a ticket with an id “CELLSNET-41654” for your issue. We will look into your issue soon.

Thank you.

I found a work-around that seems to get past the .SetStyle crash.

Unfortunately, I am still at a standstill since with this upgrade I no longer have scrollbars (see other post)

The work-around for the .SetStyle crash: rather than using a foreach() loop, I use a for() loop

for (int i = 0; i < ws.Cells.Count; i++)


WebCell cell = ws.Cells[i];

//foreach (WebCell cell in ws.Cells) //<-- crashes with new .dll



Thanks for sharing your findings.

Yes, you are right. I tested with the following sample code and it works fine regarding SetStyle exception now:

Sample code:

for(int i = 0; i< ws.Cells.Count; i++)
style = ws.Cells[i].GetStyle();
style.ForeColor = Color.Red;
ws.Cells.SetStyle(ws.Cells[i].Row, ws.Cells[i].Column, 1, 1, style);

Anyways, we will also look into it why the exception occurs when we use foreach loop.

Also, for your other issue regarding scrollbars (logged as “CELLSNET-41653”), please follow up your other thread, hopefully, we will try to figure it soon:
<a href="https://forum.aspose.com/t/94521

Thank you.


We have analyzed your issue “CELLSNET-41654” in details. Well, due to unavoidable reasons and other aspects taken into accounts, we cannot support the old way i.e.
foreach (WebCell cell in ws.Cells)


So, kindly use your devised way to apply styles to the cells, it will work for you as you already mentioned.

for (int i = 0; i < ws.Cells.Count; i++)

Thank you.