Solved

Listview, c#

Posted on 2014-12-24
6
278 Views
Last Modified: 2014-12-24
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

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40517294
Hi eghtebas;

I tried your code without issue. Have you configured the ListView correctly? This is how I configured the ListView to work. I set the following from there defaults View property to Details, I added columns by modifying the Columns property as shown in the screen shot below. Then ran the application.

Add these Columns to ListViewResults of running application.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40517302
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
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40517306
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.

Spaces in Column headers
0
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40517307
Here is the link to the new question when you have some time.

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28586670.html
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40517311
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.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40517322
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question