如何从ListViewItem获取数据; WinForms,C#

时间:2017-04-22 08:32:25

标签: c# json winforms listview listviewitem

我将一个表单中的项目添加到另一个表单上的列表视图中,并希望将listview中的每个项目保存到JSON文件中。我无法弄清楚如何做到这一点并看了好几个地方,但似乎并没有人使用我正在使用的相同方法,或者我可能做错了。

以下是我用来将项目添加到listview的代码,我如何访问每个ListViewItem中的每一段数据,以便能够将它全部保存到JSON文件中?

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);

这是Values类的信息,在此表单上命名为“Data”。

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;
        }
    }

1 个答案:

答案 0 :(得分:1)

您似乎正在存储用于填充@ECHO OFF IF EXIST "C:\EasyDent4\EdpV4\EasyDent4.exe" ( ECHO File Exists. Renaming file... ) ELSE ( ECHO file doesnt exist. Creating file from copy. GOTO Continue2 ) :Continue1 RENAME "C:\EasyDent4\EdpV4\EasyDent4.exe" EasyDent4.exe.bak && ( ECHO Renamed completed. ) :Continue2 XCOPY /I /G EasyDent4.exe C:\EasyDent4\EdpV4\ && ( ECHO Copied File to C:\EasyDent4\EdpV4 ) :Continue3 ECHO completed 属性中Values的{​​{1}}类实例:

ListViewItem

因此,您可以在需要时从那里提取它。例如:

Tag