winForms + DataGridView 绑定(bind)到 List<T>

标签 winforms data-binding

我正在尝试绑定(bind) List<T>到 DataGridView 控件,我没有任何运气来创建自定义绑定(bind)。

我试过了:

gvProgramCode.DataBindings.Add(new Binding("Opcode",code,"Opcode"));

它抛出一个异常,表示该属性名称没有找到任何内容。

有问题的列的名称是“操作码”。 List<T> 中的属性名称是操作码。

回答编辑 :问题是我的类中没有可绑定(bind)字段作为属性,只有公共(public)字段......显然它不会反射(reflect)字段,只是属性。

最佳答案

我发现的另一个解决方案是使用 BindingList 集合。



private void Form1_Load(object sender, EventArgs e)
{
   BindingList people= new BindingList {
    new Person {Name="John",Age=23},
    new Person {Name="Lucy",Age=16}
  };

   dataGridView1.DataSource= people;
}

这对我来说可以,

关于winForms + DataGridView 绑定(bind)到 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/125109/

相关文章:

c# - WinForms:在正确的位置绘制路径

c# - 在 C# 中强制关闭对话框

c# - 如何在不重新安装的情况下更新winform应用程序

c# - 使用不可选择的项目创建 WinForms ComboBox

Angular 属性绑定(bind)

c# - 简单数据绑定(bind)

c# - 使用 ASP.NET 进行条件 html 呈现

vb.net - 在 VB.NET 中将 ArrayList 数据绑定(bind)到 ListBox?

c# - 如何从 MainWindowHandle 获取窗体?

c# - CheckedListBox 数据绑定(bind)到项目的选中状态