动态对象绑定 [英] Dynamic object binding

查看:79
本文介绍了动态对象绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BindingSource将dynamic 对象绑定到DataGridView.有什么方法可以将DataGridView绑定,因此可以将其他网格绑定到dynamic 对象吗?我什至无法对列进行硬编码,也无法使绑定起作用.

更新
在谈论动态对象时,我所谈论的是一个从DynamicObject继承并具有TryGetMember方法的对象.我实际上正在尝试使用此DynamicObject绑定到字典.认为这可以在WPF中使用,但是想知道是否有人已经在Windows窗体中解决了这个问题.

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.

Update
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.

推荐答案

最后放弃了使用DynamicObject.而是创建一个数据表:

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;
        table.Rows.Add(row);
    }
    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;
        return;
    }
}


要考虑的一件事是根据此论坛线程分配的动态对象的类型,必须确保该对象实现IListIListSourceIBindingListIBindingListView,因为它在datagridview.datasource
中也能正常工作
将动态创建的属性绑定到网格 [ ^ ].

为了让人们为您提供更多帮助,您需要显示一个示例代码,该示例会给您带来问题,并可能给您使用的动态对象
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


这篇关于动态对象绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆