Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Listview, c#

Posted on 2014-12-24
6
Medium Priority
?
287 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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