select item from listview and cast to my custom object

select item from listview and cast to my custom object

c# listview get selected item value
c# listview get selected item column value
c# listview selected item index
listview selected item wpf
listview selected item event c#
wpf listview
c# listview find item by text

I'm trying to select object from listview and cast to my custom object like this

MyObject foo = (MyObject)MyListView.SelectedItems[0];

but this give's me compile error

Error 4 Cannot convert type 'System.Windows.Forms.ListViewItem' to 'MyObject'

Default ListView is not data-bindable (i.e. you can't assign some objects as data source of list view). It contains ListViewItem objects, which cannot be casted to your data type. That's why you getting this error.

If you want to get your custom object from ListViewItem then you need to construct it manually from 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 you can store custom object in Tag property of ListViewItem and get it back:

ListViewItem item = (MyObject)MyListView.SelectedItems[0];
MyObject foo = (MyObject)item.Tag;

BTW consider to use DataGridView which supports binding.

ListView.SelectedItems Property (System.Windows.Forms , Gets the items that are selected in the control. To run this example, paste the following code into a form that contains a ListView object named ListView1 and a � The ListView is named "listViewBlock" containing my Object(FDBlock) wich has several items. How I can cast this ListViewItem as my FDBlock Object? private FDBlock _fdBlockSelected;

I think you need to create your object differently, not casting in this way.

If you retrieve the text in the item, then create your object using that text.

string txt   = MyListView.SelectedItems[0].Text;
MyObject foo = new MyObject(txt);

Then use your object in the usual way. It is difficult to tell any more about what you need without more details.

ListView Class (Windows.UI.Xaml.Controls), The controls in the layout can be bound to properties of a data object, or have content Windows 8 In Windows 8, when the data item in a selected ListViewItem is Authors of custom controls use this property to change the default for the style that their control uses. Gets or sets the shadow effect cast by the element. If you are binding the RadListView properly to a collection of objects of type tblVehicle then the DataItem property of the control will hold a reference to the correct object. Note that you will need to cast the DataItem property to tblVehicle and not e.Item which is always of type RadListViewDataItem: Here is a sample code showing this approach:

Assuming your ListItems are objects of your custom class MyObject Now in the latest Xamarin, you can do it by getting SelectedItem and cast it to your object type.

public class ItemSelectionDemo
    public void ItemSelected(object sender, EventArgs e)
        MyObject item = (MyObject)MyListView.SelectedItem;
        Debug.WriteLine("Selected item Id: " +;
        Debug.Writeline("Selected item Name: " +;

public class MyObject
    public int id;
    public string name;

You can also use SelectedItemChangedEventArgs in the method and get the selected item and the cast it to your object class type.

public void ItemSelected(object sender, SelectedItemChangedEventArgs e)
    MyObject item = e.SelectedItem as MyObject;
    Debug.WriteLine("Selected item Id: " +;
    Debug.Writeline("Selected item Name: " +;

Fill a ListView with any Dataset, and perform sorting by a Column , This article will demonstrate how to perform custom sorting in the ListView control and how to handle special data-type conditions when sorting. Select the item and subitems when selection is made. object that performs the sorting of items in the ListView. Cast the objects to be compared to ListViewItem objects This listview control is populated with at times thousands of listviewitems.Part of my process, is running through custom made filters, to hide unwanted items (before they're even inserted into the listview).So as I hinted earlier, the way I hide items that I don't want is by not inserting them at all.

select item from listview and cast to my custom object -文件下载-卡卡 , The ListView in C# provides an interface to display a list of items of the ListView class, set its properties and add a ListView object to the Form controls. We can also get text associated with the currently selected item by� For a single-selection ListView, this property returns a collection containing the only selected item in the ListView. For more information on the tasks that can be performed with the items in the collection, see ListView.SelectedListViewItemCollection .

ListView in C#, Can the ObjectListView use a selection scheme like Vista? Simply tell the DataTreeListView which column holds the row id, and which holds v2.0, if you want to custom sort a FastObjectListView , you will have to subclass it handles all models as objects and it's up to you to cast them to the right type when you need to. To determine which items are selected in the ListView control, use the SelectedItems property to access the ListView.SelectedListViewItemCollection. You can create an event handler for this event to perform tasks whenever there is a change to the selected items in the ListView control.

Learning to cook — ObjectListView 2.9.1 documentation, The sender argument which is of type object, but can be cast to the RadListView type. An ItemTapEventArgs object which has a reference to the tapped item� A ListView is a list of items that creates a vertial scrollable list. A ListView can list as many items as is need for a list. In this circuit, we create a ListView that has various items. We then retrieve the values that are selected in the ListView by creating an ObservableList and then reading the data by the following code.