Click here to Skip to main content
15,069,075 members
Please Sign up or sign in to vote.
5.00/5 (4 votes)
I am trying to bind a dynamic object to a DataGridView using a BindingSource. Is there some way to bind a DataGridView, so some other grid to a dynamic object? I cannot even hard code columns and get the binding to work.

When talking about dynamic object I am talking about an object that inherits from DynamicObject, and has a TryGetMember method. I am actually trying to bind to a dictionary using this DynamicObject. Think this would work in WPF, but wondering if somebody has solved this problem in Windows Forms.
Updated 14-Aug-12 10:19am
Kenneth Haugland 14-Aug-12 15:58pm
Do you mean automatic reziesing of child object, or parent object?
Sergey Alexandrovich Kryukov 14-Aug-12 16:01pm
Don't you think the bound object is always dynamic? such as database? If not, please explain the dynamic scenario you mean.
Christian Amado 14-Aug-12 16:12pm
@Sergey we have explanation problems on questions, Right?
Kenneth Haugland 14-Aug-12 16:28pm
Find all the controls within the list, litview or whatever and rezise them?

Finally gave up using DynamicObject. Created a DataTable instead:

public static DataTable CreateTable(IEnumerable<RowValidator> model)
    var Good = imageToByteArray(Properties.Resources.Check16);
    var Bad = imageToByteArray(Properties.Resources.Block16);

    var table = new System.Data.DataTable();
    table.Columns.Add(new DataColumn("Pass", System.Type.GetType("System.Byte[]")));
    foreach (var column in model.First().ValueValidators)
        table.Columns.Add(new DataColumn(column.Key));

    foreach (var validator in model)
        var row = table.NewRow();
        row["Pass"] = validator.IsValid ? Good : Bad;
        foreach (var column in validator.ValueValidators)
            row[column.Key] = column.Value.Value;
    return table;

private static byte[] imageToByteArray(System.Drawing.Image imageIn)
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();

Then in the form do:

private void FormLoad(object sender, System.EventArgs e)
    if (_table != null)
        _bindingSource.DataSource = _table;
        dataGrid.DataSource = _bindingSource;
one thing to consider is the type of dynamic object you are assigning as according to this forum thread you have to make sure that object implements IList, IListSource, IBindingList or IBindingListView for it too work correctly in the datagridview.datasource

Bind dynamically created properties to a grid[^].

To allow people to help you more you would need to show a sample of code that is giving you the problem and possibly the dynamic object that you are using
Clifford Nelson 15-Aug-12 14:53pm
I was hoping to find out how to use dynamic objects, but this works.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900