How does data binding work with DataTable?

Viewed 2139

Suppose I have a WinForms DataGridView control and I data bind it to an IList of a custom type, like this:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

private void Form1_Load(object sender, EventArgs e)
{
    var data = new ArrayList();

    data.Add(new Person("Bob", 25));
    data.Add(new Person("Alice", 23));

    this.dataGridView1.DataSource = data;
}

This displays the list items with column names "Name" and "Age" neatly inferred (by reflection) from the public properties of the list items. (The first item according to my tests.)

But if I do the same using a DataTable:

DataTable dt = new DataTable();

dt.Columns.Add("Name");
dt.Columns.Add("Row");

dt.Rows.Add("Bob", 25);
dt.Rows.Add("Alice", 23);

this.dataGridView1.DataSource = dt;

...how does the DataGridView know how to use the DataTable's columns instead of its public properties? Neither DataTable nor DataRow seem to implement any interface supplying this information. Or does DataGridView know about the DataTable type, and treats this type of data source differently?

The reason I'm asking is because I would like to implement my own "dynamic" data source type that does not rely on fixed properties.

3 Answers
Related