简体   繁体   English

定义列表,然后绑定到数据网格视图

[英]Defining a List and then Binding to a Data Grid View

I am attempting to define a list and then assign it to a Data Grid View: 我试图定义一个列表,然后将其分配给数据网格视图:

    var list = new[]
{ 
    new { Number = 10, Name = "Smith" },
    new { Number = 10, Name = "John" } 
}.ToList();

var bindingList = new BindingList(list);
var source = new BindingSource(bindingList, null);
dgvResult.DataSource = source;

I know my problems stem from new BindingList(list) - do I need to define the type? 我知道我的问题来自新的BindingList(list)-我需要定义类型吗?

Why the BindingList? 为什么是BindingList? Following code works: 以下代码有效:

    var list = new[]
    {   
        new{Number = 10, Name="John"},
        new {Number=20, Name="Smith"}
    }.ToList();

    dataGridViewList.DataSource = list;

Hopefully it helps... 希望它可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM