AllowUserToAddRows不适用于List<> DataGridView上的数据源 [英] AllowUserToAddRows doesn't work with with List<> Datasource on DataGridView

查看:78
本文介绍了AllowUserToAddRows不适用于List<> DataGridView上的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView ,其中 DataSource 设置为 List< myClass>

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

但是,当我将 AllowUserToAddRows 设置为<$ c时,新行指示符不显示$ c> true ,

当我将 DataSource 设置为 BindingList< myClass> ,似乎可以解决问题。

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

Q :应替换我的 List<> BindingList<> 还是有更好的解决方案?

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

推荐答案

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

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

(编辑)或者-只需将列表包装在 BindingSource 中,它可能会起作用:

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

这篇关于AllowUserToAddRows不适用于List&lt;&gt; DataGridView上的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆