views:

437

answers:

1

Hi everyone,

I've DataGridView that bound to List<myClass> as DataSource.

But when i set "AllowUserToAddRows" property to "True" there is nothing appear.

I tried to change the DataSource to BindingList<myClass> and it's going well.

I wonder if i should replace my List<> with BindingList<> or there is better solution ??.

Thank you very much.

+4  A: 

Does myClass have a public parameterless constructor? If not, you could derive from BindingList<T> and override AddNewCore to call your custom constructor.

(edit) Alternatively - just wrap your list in a BindingSource and it may work:

using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class Person {
    public string Name { get; set; }

    [STAThread]
    static void Main() {
        var people = new List<Person> { new Person { Name = "Fred" } };
        BindingSource bs = new BindingSource();
        bs.DataSource = people;

        Application.Run(new Form { Controls = { new DataGridView {
            Dock = DockStyle.Fill, DataSource = bs } } });
    }
}
Marc Gravell
That worked :) Thank u very much.
Wahid Bitar