c# - select item from listview and cast to my custom object -


i'm trying select object listview , cast custom object this

myobject foo = (myobject)mylistview.selecteditems[0]; 

but give's me compile error

error 4 cannot convert type 'system.windows.forms.listviewitem' 'myobject'

default listview not data-bindable (i.e. can't assign objects data source of list view). contains listviewitem objects, cannot casted data type. that's why getting error.

if want custom object listviewitem need construct manually listviewitem:

listviewitem item = (myobject)mylistview.selecteditems[0]; myobject foo = new myobject(); foo.firstname = item.text; foo.lastname = item.subitems[1].text; foo.age = int32.parse(item.subitems[2].text); 

or can store custom object in tag property of listviewitem , back:

listviewitem item = (myobject)mylistview.selecteditems[0]; myobject foo = (myobject)item.tag; 

btw consider use datagridview supports binding.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -