Home > Enterprise >  Cast ListView Items to List<Object> in C#
Cast ListView Items to List<Object> in C#

Time:12-03

I need to store the current listview items in a new object list just after I removed an element from listview.

This is my schema.cs

public class Show
{


public class Show
 {
    public Show() { }
    public int OrdNum { get; set; }
    public DateTime DTshow { get; set; }
    public string values { get; set; }
    public int practice_Number { get; set; }

 }

}




       

The problem is in Takenshows.cs

I don't know how to cast listview items to List< Show> after deleting an element from listview.

This is the button where I press and I remove an existing element from listview:

//Takenshows.cs... public List<Show> myShows;

public TakenShows()
{

InitializeComponent();
lvwColumnSorter = new ListViewColumnSorter();
this.listView1.ListViewItemSorter = lvwColumnSorter;
myShows = new List<Show>();

}

private void button1_Click(object sender, EventArgs e)
{

c = 0;

if (listView1.SelectedItems != null)
{

    for (int i = 0; i < listView1.Items.Count; i  )
    {

        if (listView1.Items[i].Selected)
        {

            DialogResult dr = MessageBox.Show("Are you sure you want to remove the element?", "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            switch (dr)
            {

                case DialogResult.Yes:


                    listView1.Items[i].Remove();

                    i--;

                    for (int j = 0; j < listView1.Items.Count; j  )
                    {
                        c = c   1;
                        listView1.Items[j].SubItems[0].Text = c.ToString();
                    }

                    f = Int32.Parse(c.ToString());
                    // HERE's THE PROBLEM I need to cast my selected items from list view to object list ( List<Show>) and store those in myShows typed List<Show> Data
                    myShows = listView1.SelectedItems.Cast<ListViewItem>().Select(x =>
                         x.OrdNum, x.DTshow, x.values, x.practice_Number).ToList();

                    var frm2 = Application.OpenForms.OfType<Main>().First();

                    if (frm2 != null)
                    {
                        frm2.devCont();
                        frm2.devcontlist(f);
                    }

                    break;
                case DialogResult.No:
                    break;

        }
      }
    }
  }
}

would have to do something like this:

                          //BUT this code not works
            myShows = listView1.SelectedItems.Cast<ListViewItem>().Select(x =>
                                 x.OrdNum,x.DTshow,x.values,x.practice_Number).ToList();

I need when I'm removing an existing element from listview items, update the listview with the elements stayed after I removed one of them without including the element I removed.

The listview has to update after I remove an existing element from listview and it has to store in a < Show> list. How Can I do that? I've tried all possible ways but it's almost impossible.

CodePudding user response:

The ListViewItem Class does not have OrdNum, DTshow, etc. properties of your data model. You could add your model to the Tag property of the item when adding a new ListView item like this

ListViewItem item = new ListViewItem();
item.Tag = show;
item.Text = show.TheText;
//TODO: add subitems
listView1.Items.Add(item);

Then you can retrieve the data like this:

myShows = listView1.SelectedItems.Cast<ListViewItem>()
    .Select(lvi => (Show)lvi.Tag)
    .ToList();
  • Related