I have a VSTO 4 (Visual Studio 2010) document-level Excel 2007 add-in where I use a ListObject
to display data I retrieve from a WCF web service, which uses EF to fetch the data from the database. Some of the columns are Nullable<int>
(as the database column is defined with NULL
, and EF maps DBNull
to null
), and those that have null
are correctly displayed as an empty cell in the ListObject
.
The problem arises when I try to change the value of a cell that is mapped to a Nullable<int>
to null by deleting the cell's content - it immediately reverts it to the previous value (and fires the OriginalDataRestored
event). IntelliTrace tells me that underneath an exception is thrown and caught in VSTO:
System.ArgumentException occurred
Message=Object of type 'System.DBNull' cannot be converted to type 'System.Nullable`1[System.Int32]'.
Source=mscorlib
StackTrace:mscorlib.dll!System.RuntimeType.TryChangeType(object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, bool needsSpecialCast)
mscorlib.dll!System.RuntimeType.CheckValue(object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr)
mscorlib.dll!System.Reflection.MethodBase.CheckArguments(object[] parameters, System.Reflection.Binder binder, System.Reflection.BindingFlags invokeAttr, System.Globalization.CultureInfo culture, System.Signature sig)
System.dll!System.SecurityUtils.MethodInfoInvoke(System.Reflection.MethodInfo method, object target, object[] args) System.dll!System.ComponentModel.ReflectPropertyDescriptor.SetValue(object component, object value) Microsoft.Office.Tools.Excel.Implementation.dll!Microsoft.Office.Tools.Excel.ListObjectImpl.GetCellValue(object item, Microsoft.Office.Interop.Excel.Range dataRange, int rowIndex, int columnIndex, System.ComponentModel.PropertyDescriptor property, ref bool fireCellRestorationEvent, object excelValue, bool inItemAdd)
(more...)InnerException:
It seems that for some reason the null
from empty cell is converted to DBNull
, which cannot be assigned into a Nullable. How can I make the ListObject play well with Nullables?