Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Listview, c#

In a listview, the following code is expected to display in 8 rows x 4 columns the information from "Data" object. Instead, it repeats FirstName 8 times in one row.

Question: How can I make this listview to show 4 fields (FirstName, LastName, Position, Homeworld) in 8 rows?

       private void btnLoadPeopleWhile_Click(object sender, EventArgs e)
        {
            List<Person> people = Data.GetPeople();
            int counter = 0;
            while (counter < people.Count)
            {
                ListViewItem lviPerson = new ListViewItem(people[counter].FirstName);
                lviPerson.SubItems.Add(people[counter].LastName);
                lviPerson.SubItems.Add(people[counter].Position);
                lviPerson.SubItems.Add(people[counter].Homeworld);
                counter++;
                listView2.Items.Add(lviPerson);
            }
        }

//===================
       public static List<Person> GetPeople()
        {
            List<Person> people = new List<Person>();
             
            people.Add(new Person{FirstName = "Luke", LastName= "Skywalker", Position = "Jedi Knight", Homeworld = "Hw1"});
            people.Add(new Person{FirstName = "Darth", LastName= "Vader", Position = "Jedi Knight", Homeworld = "Hw2"});
            people.Add(new Person{FirstName = "Yoda", LastName= string.Empty, Position = "Jedi Master", Homeworld = "Hw3"});
            people.Add(new Person{FirstName = "Han", LastName= "Solo", Position = "Captain", Homeworld = "Hw4"});
            people.Add(new Person{FirstName = "Boba", LastName= "Fett", Position = "Bounty Hunter", Homeworld = "Hw5"});
            people.Add(new Person{FirstName = "Princess", LastName= "Leila", Position = "Heir", Homeworld = "Hw6"});
            people.Add(new Person{FirstName = "Lando", LastName= "Calrissian", Position = "General", Homeworld = "Hw7"});
            people.Add(new Person{FirstName = "Garth", LastName= "Schulte", Position = ".NET Developer", Homeworld = "Hw8"});

            return people;
        }

//=======================
   public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
        public string Homeworld { get; set; }
    }

Open in new window


The following is foreach solution but the problem is the same as described above. I need both while and foreach solutions:
       private void btnLoadPeopleForeach_Click(object sender, EventArgs e)
        {
            List<Person> people = Data.GetPeople();
            foreach (Person p in people)
            {
                ListViewItem lviPerson = new ListViewItem(new string[] { p.FirstName, p.LastName, p.Position, p.Homeworld }, 0);
                listView2.Items.Add(lviPerson);
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Hi Fernando,

I had done the same thing but instead of FirstName for example I have First Name for the text property. After I removed the blank space, it works now.

I am preparing another question where a recursive function audits files and folders, say in C:\Documents, and shows them in a treeView structure. This question will be handled possible in a few threads. The starting version will be to find out what control (if not listView) the treeView is build and where the file and folder icons could be found. These two items possibly will be tested for a simple sample like:

C:\Folder1
C:\Folder2
             -Budget.xls
             -Budget.doc
             - Subfolder1
                      -Production.xls
                     -Calculation.mdb

I will provide a link for you shortly.

Thanks,

Mike  

Thank you,

Mike
Hi eghtebas;

You stated, "I had done the same thing but instead of FirstName for example I have First Name for the text property. After I removed the blank space, it works now.", I think you mean Name property because it is used as a variable name for the item where the Text property is used to assign a header. The below screen shot shows columns headers with spaces.

User generated image
Here is the link to the new question when you have some time.

https://www.experts-exchange.com/questions/28586670/File-folder-treeview-c.html
You are correct. Text Property could be any think. It works with First Name. I must have tried with a different version of code. I have been changing it back and forth several times.
Hi Fernando,

I have located a good link to do my next project (file/folder threeView). So, the statement of my question has dramatically changed. I hope you are not spending time answering them which are no longer important.