DataGridView 源代码

DataGridView Source

我有一个包含三个变量的 class:

public class IO_ObjectMapping

{
    public String IOName = "";
    public String ObjectName = "";
    public String ObjectAttribute = "";
    public IO_ObjectMapping()
    {

    }
    public IO_ObjectMapping(String IOName, String ObjectName, String ObjectAttribute)
    {
        this.IOName = IOName;
        this.ObjectName = ObjectName;
        this.ObjectAttribute = ObjectAttribute;

    }

}

在我的主要 class 中,我创建了一个对象列表:

List<IO_ObjectMapping> lIO = new List<IO_ObjectMapping>();

然后添加对象:

        foreach (String item in IOs)
        {
            lIO.Add(new IO_ObjectMapping(item, "", ""));
        }
        foreach (String item in SCD_Objects)
        {
            lIO.Add(new IO_ObjectMapping("", item, ""));
        }

我如何填充 DataGridView,其中每一行都是一个包含三个单元格的对象?

dataGridView1.DataSource = data1;

List 没有实现 IBindingList ,使用 BindingList<T> 代替。

var bindingList = new BindingList<IO_ObjectMapping>(lIO);
//fill data
var source = new BindingSource(bindingList, null);
grid.DataSource = source;