Solved

Listview, c#

Posted on 2014-12-24
6
266 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
  • 4
  • 2
6 Comments
 
LVL 62

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 33

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 62

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 33

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 33

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 33

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now