المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Noob question: Displaying a List in a ListView



C# Programming
05-02-2009, 10:42 AM
Hi,
I want to know if this is the way to work with ListViews:

I noticed that ListViews contain strings only.. so.. if I have a List of persons List, and each Person has properties Id, Name, Age, and much more.

I want to display only name and age (there are two columns in the ListView: Name, Age),

My question:
I'd like to work with the Person (object Person!) selected.. I'm currently doing it this way:

Adding the items to the list:

foreach (Person person in personList)
{
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(person.Name);
lvi.SubItems.Add(person.Age.ToString());
lvi.SubItems.Add(person.Id.ToString());

listView1.Items.Add(lvi);
}


And the subitem Id is not listed because of "lack of column", i mean: There are two columns.. so Id is not displayed because it would need 3 columns..

Then, that way i can use the Id of the person and refer to the object..

Is this the way one would do that ? I suppose not because it doesnt look very nice..
In fact if i would add later a new column i would have to change things

Help please!