Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error with Code - Using Listview

Status
Not open for further replies.

mastertorr

Programmer
Dec 21, 2006
36
CA
Control: Microsoft ListView Control, Version 6.

ERROR:

System.InvalidCastException was unhandled
Message="Unable to cast COM object of type 'MSComctlLib.ListItemClass' to class type 'System.Windows.Forms.ListViewItem'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."
Source="Project"
StackTrace:
at Project.frmStartup.LoadListView() in C:\Documents and Settings\Ennis\My Documents\Visual Studio 2008\Projects\Project\Project\frmStartup.vb:line 24
at Project.frmStartup.frmStartup_Load(Object sender, EventArgs e) in C:\Documents and Settings\Ennis\My Documents\Visual Studio 2008\Projects\Project\Project\frmStartup.vb:line 11
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
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.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Project.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
=====================================================

Code Being used:
For i = 1 To openrec.RecordCount
lstitem = AxListView1.ListItems.Add()
lstitem.Text = openrec.Fields("field1").Value
lstitem.SubItems(1) = openrec.Fields("field1").Value
lstitem.SubItems(2) = openrec.Fields("field2").Value
openrec.MoveNext()
Next

========================================================

Anyone know why I am getting this error? I am trying to load a listview with information from my database.
 
Is lstitem declared as a ListViewItem?

I only ask as I think the problem is coming from where you're trying to assign it as an added listitem, and it can't convert between your control's class MSComctlLib.ListItemClass and what you've got lstitem declared as probably System.Windows.Forms.ListViewItem.

What version of Visual Studio are you using? This is because (and I can't vouch for versions earlier than 2005) that's not the listview control I have available (and I seem to remember it was available in Windows Common Controls through VB6?)

Regards

Andy
---------------------------------
[green]' Signature removed for testing purposes.[/green]

 
I see it is the same listview control available in VB6, accessible via the COM Components tab of the Choose Toolbox Items dialog.

Is there a particular reason you chose to use this version of the listview?

Andy
---------------------------------
[green]' Signature removed for testing purposes.[/green]

 
No particular reason why i chose this version. I think I may have had too many references selected, so I deleted/unselected some of them.

I then was able to figure out my issue using the listview (most recent version - not vb6 version). I will post how i resolved it later.

Thank you for your reply though HarleyQuinn. Its appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top