Search code examples
c#datagridviewobjectdatasource

DataGridView Source


I have a class with three variables:

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;

    }

}

In my main class I create a list of objects:

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

Then add the objects:

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

How can i populate a DataGridView where each row is one object with three cells?

dataGridView1.DataSource = data1;

Solution

  • List does not implement IBindingList , use BindingList<T> instead.

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