尝试填充ObjectListView控件时无法转换对象错误

时间:2014-02-10 00:20:08

标签: vb.net objectlistview

我已经下载了ObjectListView.dll并将其作为参考添加到我的VB.NET应用程序中。我已将它添加到我的工具箱中并向我的表单添加了ObjectListView控件。但是,当我尝试填充它时,我收到以下错误:

  

无法转换类型为#System; Winds.Forms.ListViewItem'的对象至   输入' BrightIdeasSoftware.OLVListItem'

我的代码段如下:

lsvOverdueCalls.Items.Add(tempDT.Rows(0)("id").ToString)
lsvOverdueCalls.Items(0).SubItems.Add(tempDT.Rows(0)("summary").ToString)
lsvOverdueCalls.Items(0).SubItems.Add(tempDT.Rows(0)("first_name").ToString)

不确定我是否做错了什么。我的应用程序是在dot.net 4.5的完整版本下编译的,而不是建议的客户端版本。 任何帮助表示赞赏。

更新 我注意到我的调试器窗口中出现以下错误,但仅当将鼠标移到ListViewItem上时才会出现:

  

类型' System.InvalidCastException'的第一次偶然异常。   发生在ObjectListView.dll System.Transactions中严重:0:   http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled未处理   exceptionDrakeMon2.0.vshost.exeSystem.InvalidCastException,   mscorlib,版本= 4.0.0.0,文化=中性,   PublicKeyToken = b77a5c561934e089无法投射   类型对象System.Windows.Forms.ListViewItem'输入   ' BrightIdeasSoftware.OLVListItem&#39 ;.在   BrightIdeasSoftware.ObjectListView.GetItem(Int32 index)at   BrightIdeasSoftware.ObjectListView.LowLevelHitTest(Int32 x,Int32 y)
  在BrightIdeasSoftware.ObjectListView.OlvHitTest(Int32 x,Int32 y)
  在BrightIdeasSoftware.ObjectListView.BuildCellEvent(CellEventArgs   args,Point location)at   BrightIdeasSoftware.ObjectListView.OnMouseMove(MouseEventArgs e)at   System.Windows.Forms.Control.WmMouseMove(Message& amp; m)at   System.Windows.Forms.Control.WndProc(Message& amp; m)at   System.Windows.Forms.ListView.WndProc(Message& amp; m)at   BrightIdeasSoftware.ObjectListView.WndProc(Message& amp; m)at   System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,   Int32 msg,IntPtr wparam,IntPtr lparam)at   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&放大器;放大器;   msg)at   System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr的   dwComponentID,Int32 reason,Int32 pvLoopData)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32   reason,ApplicationContext context)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32   reason,ApplicationContext context)at   System.Windows.Forms.Form.ShowDialog(IWin32Window所有者)at   DrakeMon2.frmNewDashboard.ToolStripMenuItem2_Click(对象发送者,   EventArgs e)在C:\ Dropbox \ VS Projects \ DrakeMon v2.0 \ DrakeMon中   v2.0 \ frmNewDashboard.vb:第986行   System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)at at   System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)at   System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
  在System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)at   System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
  在System.Windows.Forms.Control.WmMouseUp(消息& amp; m,   MouseButtons按钮,Int32点击)at   System.Windows.Forms.Control.WndProc(Message& amp; m)at   System.Windows.Forms.ToolStrip.WndProc(Message& amp; m)at   System.Windows.Forms.ToolStripDropDown.WndProc(Message& amp; m)
  在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,   Int32 msg,IntPtr wparam,IntPtr lparam)at   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&放大器;放大器;   msg)at   System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr的   dwComponentID,Int32 reason,Int32 pvLoopData)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32   reason,ApplicationContext context)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32   reason,ApplicationContext context)at   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()   在   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()   在   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[]   commandLine)在DrakeMon2.My.MyApplication.Main(String [] Args)中   17d14f5c-a337-4978-8281-53493378c1071.vb:第81行   System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String []   args)at   System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext   activationContext,String [] activationCustomData)at   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()   在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态)at   System.Threading.ThreadHelper.ThreadStart()System.InvalidCastException:   无法转换类型为#System; Winds.Forms.ListViewItem'的对象至   输入' BrightIdeasSoftware.OLVListItem'。在   BrightIdeasSoftware.ObjectListView.GetItem(Int32 index)at   BrightIdeasSoftware.ObjectListView.LowLevelHitTest(Int32 x,Int32 y)
  在BrightIdeasSoftware.ObjectListView.OlvHitTest(Int32 x,Int32 y)
  在BrightIdeasSoftware.ObjectListView.BuildCellEvent(CellEventArgs   args,Point location)at   BrightIdeasSoftware.ObjectListView.OnMouseMove(MouseEventArgs e)at   System.Windows.Forms.Control.WmMouseMove(Message& amp; m)at   System.Windows.Forms.Control.WndProc(Message& amp; m)at   System.Windows.Forms.ListView.WndProc(Message& amp; m)at   BrightIdeasSoftware.ObjectListView.WndProc(Message& amp; m)at   System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,   Int32 msg,IntPtr wparam,IntPtr lparam)at   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&放大器;放大器;   msg)at   System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr的   dwComponentID,Int32 reason,Int32 pvLoopData)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32   reason,ApplicationContext context)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32   reason,ApplicationContext context)at   System.Windows.Forms.Form.ShowDialog(IWin32Window所有者)at   DrakeMon2.frmNewDashboard.ToolStripMenuItem2_Click(对象发送者,   EventArgs e)在C:\ Dropbox \ VS Projects \ DrakeMon v2.0 \ DrakeMon中   v2.0 \ frmNewDashboard.vb:第986行   System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)at at   System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)at   System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
  在System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)at   System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
  在System.Windows.Forms.Control.WmMouseUp(消息& amp; m,   MouseButtons按钮,Int32点击)at   System.Windows.Forms.Control.WndProc(Message& amp; m)at   System.Windows.Forms.ToolStrip.WndProc(Message& amp; m)at   System.Windows.Forms.ToolStripDropDown.WndProc(Message& amp; m)
  在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,   Int32 msg,IntPtr wparam,IntPtr lparam)at   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&放大器;放大器;   msg)at   System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr的   dwComponentID,Int32 reason,Int32 pvLoopData)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32   reason,ApplicationContext context)at   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32   reason,ApplicationContext context)at   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()   在   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()   在   Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[]   commandLine)在DrakeMon2.My.MyApplication.Main(String [] Args)中   17d14f5c-a337-4978-8281-53493378c1071.vb:第81行   System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String []   args)at   System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext   activationContext,String [] activationCustomData)at   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()   在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态)at   System.Threading.ThreadHelper.ThreadStart()

我假设有一个线程问题,但我不确定为什么这个控件会抛出一个错误,一个普通的listview甚至BetterListView控件都没有。我的表单只包含此控件而没有其他内容。并且...如上所述,只有当我尝试将鼠标移到ListViewItem上时才会发生。

由于

1 个答案:

答案 0 :(得分:1)

您不能像Add() ObjectListView那样ListView,就像在普通SetObjects上一样。相反,您需要在ObjectListView实例上调用List<Call>方法,并向其传递要添加的项目列表。

因此,在您的情况下,请创建要添加的Call项(假设您的模型是idsummaryfirst_namelsvOverdueCalls.SetObjects()字段,然后将其传递给var lstCalls = new List<Call>() { new Call() { Id = tempDT.Rows(0)("id").ToString, Summary = tempDT.Rows(0)("summary").ToString, First_Name = tempDT.Rows(0)("first_name").ToString } }; lsvOverdueCalls.SetObjects(lstCalls); ,就像这样。

Call

以下是public class Call { public string Id { get; set; } public string Summary { get; set; } public string First_Name { get; set; } } 模型的样子。

{{1}}