Solved

Listview, c#

Posted on 2014-12-24
6
264 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 33

Author Closing Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

21 Experts available now in Live!

Get 1:1 Help Now