List<Person> persons = new List<Person>();
persons.Add(new Person(){Id = 0,
Birthday = DateTime.Now,
Name="Jack"});
persons.Add(new Person(){Id = 1,
Birthday = DateTime.Now,
Name = "Anna"});
persons.Add(new Person(){Id = 2,
Birthday = DateTime.Now,
Name = "John"});
persons.Add(new Person(){Id = 3,
Birthday = DateTime.Now,
Name = "Sally"});
persons.Add(new Person(){Id = 4,
Birthday = DateTime.Now,
Name = "Robert"});
objectListView1.SetObjects(persons);
我想要做的是将对象列表视图中的排序值复制到文本框,其显示顺序与显示的顺序相同,但它们的显示顺序与添加的顺序相同列表&#34;人员&#34;。
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
foreach (var person in objectListView1.Objects)
{
Person p = person as Person;
textBox1.Text += p.Id + "\t" + p.Name + "\t" + p.Birthday.ToShortDateString() + "\r\n";
}
}
这是显示当前问题的示例应用的几张图片: Picture 1 Picture2
有没有人知道怎么做?
编辑:我在这个项目中使用了dll: www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView
答案 0 :(得分:0)
列表按照添加项目的顺序排序。我猜测显示器是按名称排序的。您可以通过获取最后一个排序列,以与显示它们相同的方式对它们进行排序。我认为我有正确的属性来获取列的名称/顺序。
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
List<Person> people = objectListView1.Objects.Cast<Person>().ToList();
if (objectListView1.LastSortColumn != null)
{
// check to see if this "Name" property is the right one
switch (objectListView1.LastSortColumn.Name)
{
case "Name":
if (objectListView1.LastSortOrder == SortOrder.Descending)
people = people. OrderByDescending(o => o.Name).ToList();
else
people = people.OrderBy(o => o.Name).ToList();
break;
case "Id":
if (objectListView1.LastSortOrder == SortOrder.Descending)
people = people. OrderByDescending(o => o.Id).ToList();
else
people = people.OrderBy(o => o.Id).ToList();
break;
default:
break;
}
}
foreach (var person in people)
{
Person p = person as Person;
textBox1.Text += p.Id + "\t" + p.Name + "\t" + p.Birthday.ToShortDateString() + "\r\n";
}
}
答案 1 :(得分:0)
迟到总比没有好:我认为这更可靠:
const joe = { // (A) joe is an object
id: 42,
name: 'joe',
hobbies: [ // (B) hobbies is an Array, a reference type
'surfing',
'videogames'
]
}
// (C) this variable has no idea that it is part of person object from (A)
const cloneHobbies = joe.hobbies
cloneHobbies.push('boxing')
console.log('***JOE***', joe) // Joe now has a new hobby: boxing
// It also works the other way; Joe's hobbies become someone else's
joe.hobbies.push('karate')
console.log("***CLONE'S HOBBIES***", cloneHobbies) // now has 'karate'
如果无法转换其所有列表项,则仅返回一个空列表。