2014-02-10 37 views
2

我已經下載了ObjectListView.dll並將其作爲參考添加到我的VB.NET應用程序中。我已經將它添加到我的工具箱中,並向我的表單添加了一個ObjectListView控件。然而,當我嘗試填充它,我得到以下錯誤:嘗試填充ObjectListView控件時無法投射對象錯誤

Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type '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的移動鼠標,只有當:

A first chance exception of type 'System.InvalidCastException' occurred in ObjectListView.dll System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled Unhandled exceptionDrakeMon2.0.vshost.exeSystem.InvalidCastException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'BrightIdeasSoftware.OLVListItem'. at BrightIdeasSoftware.ObjectListView.GetItem(Int32 index) at BrightIdeasSoftware.ObjectListView.LowLevelHitTest(Int32 x, Int32 y)
at BrightIdeasSoftware.ObjectListView.OlvHitTest(Int32 x, Int32 y)
at BrightIdeasSoftware.ObjectListView.BuildCellEvent(CellEventArgs args, Point location) at BrightIdeasSoftware.ObjectListView.OnMouseMove(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseMove(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ListView.WndProc(Message& m) at BrightIdeasSoftware.ObjectListView.WndProc(Message& 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 owner) at DrakeMon2.frmNewDashboard.ToolStripMenuItem2_Click(Object sender, EventArgs e) in C:\Dropbox\VS Projects\DrakeMon v2.0\DrakeMon v2.0\frmNewDashboard.vb:line 986 at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& 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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at DrakeMon2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()System.InvalidCastException: Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'BrightIdeasSoftware.OLVListItem'. at BrightIdeasSoftware.ObjectListView.GetItem(Int32 index) at BrightIdeasSoftware.ObjectListView.LowLevelHitTest(Int32 x, Int32 y)
at BrightIdeasSoftware.ObjectListView.OlvHitTest(Int32 x, Int32 y)
at BrightIdeasSoftware.ObjectListView.BuildCellEvent(CellEventArgs args, Point location) at BrightIdeasSoftware.ObjectListView.OnMouseMove(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseMove(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ListView.WndProc(Message& m) at BrightIdeasSoftware.ObjectListView.WndProc(Message& 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 owner) at DrakeMon2.frmNewDashboard.ToolStripMenuItem2_Click(Object sender, EventArgs e) in C:\Dropbox\VS Projects\DrakeMon v2.0\DrakeMon v2.0\frmNewDashboard.vb:line 986 at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& 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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at DrakeMon2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

我假設一個線程問題,但我不能確定爲什麼這個控件拋出一個錯誤和一個普通的listview,甚至是BetterListView控件都沒有。我的表單只包含這個控件,沒有別的。和...一樣,只有當我嘗試將鼠標移動到ListViewItem上時纔會發生。

感謝

+0

我不認爲它與ListViewItems ...你需要閱讀的文檔......見2.2節: http://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView – Plutonix

回答

1

你不能做Add()ObjectListView就像你在一個正常的ListView做。相反,您需要調用ObjectListView實例上的SetObjects方法,並將它傳遞給要添加的項目列表。

所以你的情況,建立項目的List<Call>增加(假設你的模型是一個Callidsummaryfirst_name字段,然後把它傳遞到lsvOverdueCalls.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; } 
} 
+0

謝謝溼婆。我已經將您的代碼轉換爲VB並具有以下內容:Dim lstCalls = New List(Of [Call_Details])()從{New [Call_Details]()With {.Id = tempDT.Rows(x)(「id」)。 ToString,.Summary = tempDT.Rows(x)(「summary」)。ToString,.First_Name = tempDT.Rows(x)(「first_name」)。ToString}} lsvOverdueCalls.SetObjects(lstCalls)。這現在刪除了我的所有錯誤,但是我的ObjectListView現在是空的。我可以看到我的lstCalls中有數據。 – Riples

+1

好了解決此問題:[鏈接](http://stackoverflow.com/questions/7949​​887/how-to-add-a-new-item-into-objectlistview)我沒有添加AspectName的細節,我的ObjectListView控件。 – Riples

+0

很高興解決。對不起,我忘了問題是在VB.NET中:) – Shiva

相關問題