简体   繁体   中英

How to get data from ListViewItem; WinForms, C#

I am adding an item from one form to a listview on a different form and want to save each item within the listview to a JSON file. I can't figure out how to do this and have looked several places but it doesn't seem like people are using the same method(s) I'm using, or maybe I'm doing it all wrong.

Here is the code I am using to add the item to the listview, how could I access each piece of data within each ListViewItem to be able to save it all to a JSON file?

ListView lv = (ListView)lf.Controls["mainListView"];
        ListViewItem lvi = new ListViewItem();
        lvi.Text = Data.Name;
        lvi.Tag = Data;
            switch (Data.Type)
            {
                case "Anime":
                    lvi.ImageIndex = 0;
                    break;
                case "Book":
                    lvi.ImageIndex = 1;
                    break;
                case "Comic":
                    lvi.ImageIndex = 2;
                    break;
                case "Game":
                    lvi.ImageIndex = 3;
                    break;
                case "Manga":
                    lvi.ImageIndex = 4;
                    break;
                case "Movie":
                    lvi.ImageIndex = 5;
                    break;
                case "Other":
                    lvi.ImageIndex = 6;
                    break;
                case "TV":
                    lvi.ImageIndex = 7;
                    break;
            }
        lv.Items.Add(lvi);

And here is the information to the Values class, which is named "Data" on this form.

public Values Data
    {
        get
        {
            Values v = new Values();
            v.Name = itemNameTxt.Text;
            v.Type = itemTypeCmb.Text;
            v.TypeName = typeNameTxt.Text;
            v.Status = statusCmb.Text;
            v.Completed = completedNUD.Value;
            v.URL = imageUrlTxt.Text;
            v.RateOne = rating1.Checked;
            v.RateTwo = rating2.Checked;
            v.RateThree = rating3.Checked;
            v.RateFour = rating4.Checked;
            v.RateFive = rating5.Checked;

            return v;
        }
        set
        {
            itemNameTxt.Text = value.Name;
            itemTypeCmb.Text = value.Type;
            typeNameTxt.Text = value.TypeName;
            statusCmb.Text = value.Status;
            completedNUD.Value = value.Completed;
            imageUrlTxt.Text = value.URL;
            rating1.Checked = value.RateOne;
            rating2.Checked = value.RateTwo;
            rating3.Checked = value.RateThree;
            rating4.Checked = value.RateFour;
            rating5.Checked = value.RateFive;
        }
    }

It seem that you are storing the Values class instance used to populate the ListViewItem inside the Tag property:

lvi.Tag = Data;

So you can extract it from there when needed. For instance:

ListView lv = ...;
var valueList = lv.Items.Cast<ListViewItem>()
    .Select(lvi => (Values)lvi.Tag)
    .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM