c# - AllowUserToAddRows 不适用于 DataGridView 上的 List<> 数据源

标签 c# .net winforms datagridview bindinglist

我有一个 DataGridViewDataSource设置为 List<myClass>

但是,当我设置 AllowUserToAddRows 时,新行指示器不显示至 true ,

当我设置 DataSourceBindingList<myClass> ,这似乎解决了问题。

:应该替换我的 List<>BindingList<>或者有更好的解决方案?

最佳答案

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

(edit) 或者 - 只需将您的列表包装在 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 } } });
    }
}

关于c# - AllowUserToAddRows 不适用于 DataGridView 上的 List<> 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1565875/

相关文章:

c# - 在 C# 中定义 extern "C"函数

c# - 使主窗口处于非事件状态的选项卡上的 WebBrowser 控件

c# - 你如何制作一个包含多个 "Pages"的程序?

c# - 陈旧元素引用 : element is not attached to the page document coded UI - C#

C# MVC 4 ASP.net,如何将文件插入数据库存储(二进制)

.net - .NET任务计划程序支持多少个处理器核心

.net - 一个窗口中有多个Web浏览器 session /进程

.net - 我们不应该在 WinForms 应用程序中使用 System.Web.Caching.Cache 类吗?

C# 抽象类实现

c# - 有没有办法从托管 64 位代码加载 32 位 DLL,而不将托管代码降级到 32 位?