我正在将excel文件导入DataGridView,我必须获得某些列的平均值。当我在大约300行的示例excel文件上尝试它时,一切正常。但是当我尝试使用41000行的实际excel文件时,我得到错误"指定的强制转换无效"当我点击按钮来计算平均值时。我真的不知道什么是错的,不知道如何解决它。这是我用来获得平均值的代码,错误在我分组的列上。
private void Ave_Click(object sender, EventArgs e)
{
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = (from r in dt.AsEnumerable()
group r by new
{
RNCID = r.Field<Double?>("RNCID"),
Date = r.Field<DateTime>("Date"),
WBTSNAME = r.Field<String>("WBTSNAME")
} into g
select new
{
g.Key.Date,
g.Key.RNCID,
g.Key.WBTSNAME,
VSLCDLMeanLicenseGroupShare = g.Average(r => r.Field<Double>("VS LC DLMean LicenseGroup Share")),
VSLCDLCreditAvailableShared = g.Average(r => r.Field<Double>("VS LC DLCreditAvailable Shared")),
VSLCULMeanLicenseGroupShare = g.Average(r => r.Field<Double>("VS LC ULMean LicenseGroup Share")),
VSLCULCreditAvailableShared = g.Average(r => r.Field<Double>("VS LC ULCreditAvailable Shared")),
UtilDL = g.Average(r => r.Field<Double?>("Util DL")),
UtilUL = g.Average(r => r.Field<Double?>("Util UL")),
});
dataGridView1.DataSource = bindingSource1;
}
这是我得到的堆栈跟踪
System.InvalidCastException was unhandled
消息=&#34;指定的演员表无效。&#34;
源=&#34; System.Data.DataSetExtensions&#34;
堆栈跟踪:
at System.Data.DataRowExtensions.UnboxT 1.NullableField[TElem](Object value)
at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
at CE_Fin.Form1.<Ave_Click>b__0(DataRow r) in C:\Users\Administrator\Documents\Visual Studio 2008\Projects\Final\CE_Fin\Form1.cs:line 215
at System.Linq.Lookup
2.Create [TSource](IEnumerable 1 source, Func
2 keySelector,Func 2 elementSelector, IEqualityComparer
1 comparer)
在System.Linq.GroupedEnumerable 3.GetEnumerator()
at System.Linq.Enumerable.WhereSelectEnumerableIterator
2.MoveNext()
在System.Windows.Forms.BindingSource.GetListFromEnumerable(IEnumerable enumerable)
在System.Windows.Forms.BindingSource.ResetList()
在System.Windows.Forms.BindingSource.set_DataSource(对象值)
at CE_Fin.Form1.Ave_Click(Object sender,EventArgs e)位于C:\ Users \ Administrator \ Documents \ Visual Studio 2008 \ Projects \ Final \ CE_Fin \ Form1.cs:第214行
在System.Windows.Forms.Control.OnClick(EventArgs e)
在System.Windows.Forms.Button.OnClick(EventArgs e)
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在System.Windows.Forms.Control.WmMouseUp(消息&amp; m,MouseButtons按钮,Int32点击)
在System.Windows.Forms.Control.WndProc(消息&amp; m)
在System.Windows.Forms.ButtonBase.WndProc(消息&amp; m)
在System.Windows.Forms.Button.WndProc(消息&amp; m)
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)
在System.Windows.Forms.Application.Run(Form mainForm)
在C:\ Users \ Administrator \ Documents \ Visual Studio 2008 \ Projects \ Final \ CE_Fin \ Program.cs中的CE_Fin.Program.Main():第18行
在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)
在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
InnerException:
答案 0 :(得分:0)
可能的原因是其中一个字段具有空值:
r.Field<Double>("VS LC DLMean LicenseGroup Share")
r.Field<Double>("VS LC DLCreditAvailable Shared")
r.Field<Double>("VS LC ULMean LicenseGroup Share")
r.Field<Double>("VS LC ULCreditAvailable Shared")
让它们成为可空<double?>
(正如您对"Util DL"
和"Util UL"
所做的那样)