Search code examples
c#objectlistview

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);

What I want to do is to copy sorted values from object list view to text box in the same order as they are displayed, but they are always displayed in the same order as they are added in list "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";
    }
  }

This are few pictures of example app to show current problem: Picture 1 Picture2

Does anyone have any idea how to do that ?

Edit: I used dll from this project: www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView


Solution

  • Lists are ordered in the order that items are added. I am guessing the display is sorting them by name. You could just sort them the same way they are being displayed by getting the last sort column. I think I have the correct properties for getting the name/order of the column.

      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";
        }
      }