DataGridView控件用DataSource=对象赋值时 对象下边还有一个对象要怎么把值输出出来呢?

如图有一个data对象,data对象的字段有一个Belongs_to_doctor对象。请问DataGridView控件用DataSource=data 赋值时 ,怎么才能显示Belongs_to_doctor对象的字段?

img

给Belongs_to_doctor添加 [Browsable(false)]属性不绑定到datagriview,然后新增一个只读string类型的属性返回Belongs_to_doctor 要显示的内容。
简单示例如下

img

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<Data> data = new List<Data>() {
            new Data{ Addr="地址",obj=new Obj{  Name="姓名1"} }
            };
            dataGridView1.DataSource = data;
        }
    }
    class Obj
    {
        public string Name { get; set; }
        public new string ToString() { return Name; }
    }
    class Data {
        [Browsable(false)]
        public Obj obj { get; set; }
        public string Addr { get; set; }
        public string ObjValue { get { return obj.ToString(); } }
    }
}


img


有帮助或启发麻烦点下【采纳该答案】,谢谢~~