简体   繁体   中英

C# Add items to listView

Hello i need help adding items to my listview im using this:

private void btnSubmit_Click(object sender, EventArgs e)
        {
            Person newPerson = new Person()
            {
                Name = txtName.Text,
                Age = txtAge.Text,
                Gender = txtGender.Text,
                Photo = txtPhoto.Text
            };
            string[] row = { txtName.Text, txtAge.Text, txtGender.Text, txtPhoto.Text};
            ListViewItem item = new ListViewItem(row);
            lvPersons.Items.Add(item);
        }

This is working but i want to use my class person to add and not this line:

string[] row = { txtName.Text, txtAge.Text, txtGender.Text, txtPhoto.Text};

because using the line above im not making use of this: (i think)

Person newPerson = new Person()
            {
                Name = txtName.Text,
                Age = txtAge.Text,
                Gender = txtGender.Text,
                Photo = txtPhoto.Text
            };

Can someone help me adding items with my class i have tested with this:

string[] row = { Name, Age, Gender, Photo };

and this

string[] row = {Person Name, Person  Age, Person  Gender, Person  Photo };

but both give me errors i need some help

It's working with:

ListViewItem item = new ListViewItem(new [] {newPerson.Name, newPerson.Age, newPerson.Gender, newPerson.Photo});

Thanks to @René Vogt for the help/solution.

try this,

            string[] row = { "1000", "2000", "3000", "4000" };
            List<string> lvPersons = new List<string>();

            foreach (var item in row)
            {
                lvPersons.Add(item);
            }

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