gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:19:54 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com