Hi,
while working with the Infragistics Excel Engine in ASP.NET, especially with styles, I stumbled upon an exception "Unable to cast object of type 'Infragistics.Documents.Excel.WorkbookUserDefinedStyle' to type [...]". Besides the facts that I made a mistake in my code and that a user defined style can be cast as a WorkbookStyle like here:
WorkbookStyle style = (WorkbookStyle)workBook.Styles["StyleName"];
I have two questions:
Just because I'm curious...
WorkbookUserDefinedStyle is an internal type. The error message you are seeing is likely a bug in the Excel library. I have forwarded this post to the Developer Support Manager and a DS engineer will be contacting you about this issue.
With regard to your questions, the WorkbookUserDefinedStyle is not exposed because it does not provide any useful functionality beyond that of WorkbookStyle (its base class). It is simple used to store a custom name, which a built-in style does not need to store, and which is accessible via the WorkbookStyle.Name property.
Hello bernhardus,
Is it possible for you to share the stack trace of the exception you are getting that will help us to dig in to this issue more promptly?
Thank you in advance.
of course I could, but I'll give you the code which provoked the exception:
public static void ProvokeWorkbookUserDefinedStyleCastException() {
Workbook wb = new Workbook();string udsName = "AUserDefinedStyle";IWorksheetCellFormat cf = wb.CreateNewWorksheetCellFormat();cf.Locked = ExcelDefaultableBoolean.True;wb.Styles.AddUserDefinedStyle(cf, udsName);IWorksheetCellFormat style = (IWorksheetCellFormat)wb.Styles[udsName];
}
Execution of the last statement will result in an InvalidCastException: Unable to cast object of type 'Infragistics.Documents.Excel.WorkbookUserDefinedStyle' to type 'Infragistics.Documents.Excel.IWorksheetCellFormat'.
As I mentioned in my post I made this mistake in my code. I got puzzled a bit by adding a cell format item to the styles collection via AddUserDefinedStyle() and not getting it back simply using a cast like in the code above. Then I realized that a style is not a cell format but has one as a property and everything worked fine.
I wonder if you can do anything to avoid a invalid cast exception on the user side (from your point of view)... If you can I really would like to know how.
Well, first of all, AddUserDefinedStyle returns you the created WorkbookStyle instance, so if you want to get it back, just cache the result. Second, WorkbookStyle does not implement IWorksheetCellFormat, so you are supposed to get an invalid cast exception when trying to cast it to one. Just because the style owns an IWorksheetCellFormat does not mean it can be cast to one, just as you cannot cast WorkbookStyle to a string instance even though it also owns a string (its Name). As you mentioned, you can get the StyleFormat property to get the associated IWorksheetCellFormat.
So I'm not really sure I understand your question: "I wonder if you can do anything to avoid a invalid cast exception on the user side (from your point of view)... If you can I really would like to know how." Are you asking for an explicit conversion operator from WorkbookStyle to IWorksheetCellFormat so you can cast it to get the StyleFormat instead of using the property? If not, can you clarify what you are looking for?
I think it's a misunderstanding, maybe because of a bad English of mine. My last post was a reply to Bhadresh Patel to support him with some code just as he asked for.
Concerning my question: This was a mere rhetorical question. I don't think one can avoid "invalid cast" exceptions which result from wrong usage of one's class library. But if you know how to prevent the users of your class library from evoking these exceptions, then please let me know how you do that. And without being disrespectful: I don't expect this question to be answered. Just in case...
So I'm not looking for anymore answers in this thread because you already answered my question - thanks for that.