0

I've got a basic Person class defined like this:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        this.Name = name;
        this.Age = age;
    }
}

Now I'm creating a list of people like this:

private List<Person> people = new List<Person>();
people.Add(new Person("John Smith", 21));
people.Add(new Person("Bob Jones", 30));
people.Add(new Person("Mike Williams", 35));

Once my list has been populated, I want to sort it by name like this:

// make sure that the list of people is sorted before assigning to bindingList
people.Sort((person1, person2) => person1.Name.CompareTo(person2.Name));

Next, I'm creating a BindingList which I will use as the datasource for a combobox like this:

private BindingList<Person> bindingList = new BindingList<Person>(people);

comboBoxPeople.DataSource = bindingList;
comboBoxPeople.DisplayMember = "Name";
comboBoxPeople.ValueMember = "Name";

So far, this much is working ok. But now I have a couple of problems that I can't seem to get fixed. First, I need to be able to add Person objects and have the list remain sorted. Right now, I can add a new Person object to the bindingList (via bindingList.Add(newPerson)) and it will show up in the comboBox, albeit at the bottom (i.e., not sorted). How can I re-sort the bindingList once I've added something to it so that it appears sorted in the comboBox?


Tableless model in rails 3.1

Looks like this method doesn't work anymore in rails 3.1. So, does someone have a working solution?

Actually, I've found this gist. It solves problems with columns_hash and column_defaults errors from the railscast's solution but I get ActiveRecord::ConnectionNotEstablished error all the time when I try to write some attribute.

Any thoughts?

4

1 に答える 1

0

正直なところ、これには特別なリストは必要ないと思います。手に持っているものを使ってください。つまり、車輪を再発明しないでください。

あなたと解決策に似た質問があります:

List<T>.Sort() のように動作する BindingList<T>.Sort()

于 2011-09-02T05:44:24.820 に答える