DataGridView-使用DataPropertyName显示子元素属性 [英] DataGridView - Use DataPropertyName to show child element property

查看:124
本文介绍了DataGridView-使用DataPropertyName显示子元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我有以下课程的图片

public class Master
{
    public string MasterName = "Something";

    public List<Detail> details = new List<Detail>();
}

public class Detail 
{
    public string Foo = "Test";
}

然后我想在DataGridView中显示Details对象的集合,使用下面的代码

And then I want to show the collection of Details objects in a DataGridView, using the code below

DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Details.Foo";
column.HeaderText = "Foo header";

dgv.Columns.Add(column);

该列显示在网格中,但没有值

The column is shown in the grid, but without value

推荐答案

您可以在实体子项中覆盖ToString方法,例如:

You can override ToString method in the entity child for example:

public class FormulariosENT {

    #region PROPERTIES

    public int IdFromulario { get; set; }
    public string DescripcionFormulario { get; set; }

    #endregion

    #region PUBLIC METHODS
    public override string ToString() {

        return DescripcionFormulario;
    }

然后绑定实体子名称。

这篇关于DataGridView-使用DataPropertyName显示子元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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