views:

251

answers:

1

I'm currently learning to develop for the .net compact framework using c# in VS2008 and have a databinding query. The list binds fine in Form1_Load, however when I add additional people to the list they don't appear in dataGrid1 (although if I remove and re-add the binding they do appear). Is there something that I need to do after I add a person?

    class Person
    {
        private string firstname;
        private string surname;

        public string FirstName { get { return firstname; } set { firstname = value; } }
        public string Surname { get { return surname; } set { surname = value; } }

        public Person(string F, string S)
        {
            this.firstname = F;
            this.surname = S;
        }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        people.Add(new Person(tbFirstName.Text, tbSurname.Text));
    }

    class People : List<Person>
    {
    }

    People people = new People();

    private void Form1_Load(object sender, EventArgs e)
    {
        people.Add(new Person("Jim", "Jones"));
        people.Add(new Person("Al", "Hill"));
        people.Add(new Person("Darth", "Vader"));
        dataGrid1.DataSource = people;
    }
+3  A: 

Change your declaration of "people" to this:

class People : BindingList<Person> { }

Plain old List<T> doesn't have the underlying events to tell the databinding UI when the list changed. Using BindingList<T> should get you going.

Matt Hamilton
dude, you rock - answered my question in 7 minutes ;-)
Alister
much better than my solution of reassigning the DataSource :-)
tjjjohnson