简体   繁体   中英

Databinding a list in c# on Compact Framework

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM