如何通过BindingSource的映射DataGridView的内/嵌套类的属性? [英] How to map properties of inner/nested classes in DataGridView through BindingSource?

查看:166
本文介绍了如何通过BindingSource的映射DataGridView的内/嵌套类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据层的独立项目,并有两种基本类型有:

I have a separate project for Data layer and there are two basic classes there:

[Serializable]
public class NesInfo
{
    public string FileName { get; private set; }
    public string Directory { get; private set; }
    public MapperInfo MapperInfo { get; set; }
}

[Serializable]
public class MapperInfo
{
    public string Number { get; set; }
    public string Prop1{ get; set; }
    public string Prop2 { get; set; }
}

现在我希望我的DataGridView来显示这样的列:结果
[文件名] [目录] [编号] [为prop1] [Prop2]

Now I want my DataGridView to Display columns like this:
[FileName][Directory][Number][Prop1][Prop2]

我怎么能实现这个使用的BindingSource?

How I can achieve this using BindingSource?

我已经用我的DataGridView,但我的的BindingSource 而不是5列试过,我拿到3(嵌套类是像对待一列,其中内部的属性应该是有代替):

I've tried using my BindingSource in my DataGridView, but instead of 5 columns, I get 3 (the nested class is treated like one column, where the inner properties should be there instead):

和尝试添加列时我无法选择MapperInfo类的内部属性:的

And I cannot select the inner properties of MapperInfo class when trying to add columns:

推荐答案

您可以创建所有你想要的网格显示,并与现有的类映射它可以手动或使用第三方库属性的新类(恩。 AutoMapper)。然后结合新类网格

You can create a new class with all the properties that you want to be display in the grid and map it with your existing class either manually or using third-party libraries (ex. AutoMapper). Then bind the new class to Grid.

public class MyGridClass
{
    public string FileName { get; set; }
    public string Directory { get; set; }
    public string Number { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

NesInfo ni = ...

MyGridClass gc = new MyGridClass ( );
gc.FileName = ni.FileName;
gc.Directory = ni.Directory;
gc.Number = ni.MapperInfo.Number;
gc.Prop1 = ni.MapperInfo.Prop1;
gc.Prop2 = ni.MapperInfo.Prop2;

这篇关于如何通过BindingSource的映射DataGridView的内/嵌套类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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