c#的新特性,listview

本文关键字:listview 新特性 | 更新日期: 2023-09-27 18:17:48

当我去运行程序时,它是空的,没有数据显示

namespace _0000003
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            soulpets = new ListView();
            ListViewItem lvi6001 = soulpets.Items.Add("6001");
            lvi6001.SubItems.Add("Tough Ent, Rare");
            ListViewItem lvi6004 = soulpets.Items.Add("6004");
            lvi6004.SubItems.Add("Stone Fist Ent, Rare");
            ListViewItem lvi6007 = soulpets.Items.Add("6007");
            lvi6007.SubItems.Add("Healing Ent,Rare");

            Controls.Add(soulpets);

c#的新特性,listview

soulpets = new ListView();

应为soulpets.Items.Clear();

您可能需要:

通过设计器添加名称为soulpets的列表视图

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        fillListViewSoulpets();
    }
    private void fillListViewSoulpets()
    {
        soulpets.Items.Clear(); //it should already be empty
        soulpets.Items.Add(
            new ListViewItem({ "Tough", "Ent", "Rare" }),
            new ListViewItem({ "Stone", "Fist", "Ent", "Rare" }),
            new ListViewItem({ "Healing", "Ent", "Rare" }) 
        );
    }
}