对象列表 查看如何按显示顺序复制值
Object List View how to copy values in their displayed order
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);
我想做的是将排序后的值从对象列表视图复制到文本框,它们的显示顺序与它们显示的顺序相同,但它们总是以相同的顺序显示因为它们被添加到列表 "persons".
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
列表按照添加项目的顺序排列。我猜显示正在按名称对它们进行排序。您可以通过获取最后一个排序列来按照显示它们的方式对它们进行排序。我认为我具有获取列的 name/order 的正确属性。
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";
}
}
迟到总比不到好:我认为这更可靠:
public static IEnumerable<T> GetObjectsOrderedAsDisplayed<T>(this ObjectListView olv) where T : class
{
IList<OLVListItem> list = new List<OLVListItem>();
for (int i = 0; i < olv.GetItemCount(); i++)
{
list.Add(olv.GetNthItemInDisplayOrder(i));
}
return list.Select(x => x.RowObject as T).Where(x => x != null);
}
如果它无法转换所有列表项,它只是 returns 一个空列表。
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);
我想做的是将排序后的值从对象列表视图复制到文本框,它们的显示顺序与它们显示的顺序相同,但它们总是以相同的顺序显示因为它们被添加到列表 "persons".
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
列表按照添加项目的顺序排列。我猜显示正在按名称对它们进行排序。您可以通过获取最后一个排序列来按照显示它们的方式对它们进行排序。我认为我具有获取列的 name/order 的正确属性。
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";
}
}
迟到总比不到好:我认为这更可靠:
public static IEnumerable<T> GetObjectsOrderedAsDisplayed<T>(this ObjectListView olv) where T : class
{
IList<OLVListItem> list = new List<OLVListItem>();
for (int i = 0; i < olv.GetItemCount(); i++)
{
list.Add(olv.GetNthItemInDisplayOrder(i));
}
return list.Select(x => x.RowObject as T).Where(x => x != null);
}
如果它无法转换所有列表项,它只是 returns 一个空列表。