AllowUserToAddRows doesn't work with with List<> Datasource on DataGridView
Asked Answered
T

1

13

I have a DataGridView with the DataSource set to List<myClass>

However, the new row indicator does not display when I set AllowUserToAddRows to true,

When I set the DataSource to BindingList<myClass>, that seems to solve the problem.

Q: Should replace my List<> with BindingList<> or there is better solution?

Turbosupercharger answered 14/10, 2009 at 12:12 Comment(0)
A
23

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 } } });
    }
}
Aster answered 14/10, 2009 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.