无法允许用户使用List <> Datasource将行添加到DataGridView

Wah*_*tar 13 .net c# datagridview bindinglist winforms

我已经DataGridView是必然DataSourceList<myClass>.但是当我将" AllowUserToAddRows"属性设置为"True"时,没有任何东西出现.

我试图改变trueDataSource,它的顺利.

我不知道我是否应该取代我BindingList<myClass>List<>或有更好的解决方案.

Mar*_*ell 22

是否myClass有一个公共的无参数构造函数?如果没有,您可以派生BindingList<T>并覆盖AddNewCore以调用您的自定义构造函数.

(编辑)或者 - 只需将您的列表包装在一个BindingSource,它可能会工作:

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 } } });
    }
}
Run Code Online (Sandbox Code Playgroud)