简体   繁体   English

C#Winforms-将DataGrid列绑定到嵌套对象中的属性

[英]C# Winforms - Bind DataGrid Columns to properties in nested objects

I have a datagrid that displays a list of custom objects. 我有一个显示自定义对象列表的数据网格。 This list contains objects who's properties are storing other objects: 此列表包含其属性正在存储其他对象的对象:

class Survey
{
 public int Id{get;set;}
 public Answer ChosenAnswer{get;set;}
 public Question SomeQuestion{get;set;}
}

Right now the grid simply gives me the type of the objects stored in Survey properties. 现在,网格只是为我提供了存储在Survey属性中的对象的类型。

So if I have a list of Survey, for example, how would I go about binding columns of my data grid to properties of Answer? 因此,例如,如果我有一个Survey的列表,我该如何将数据网格的列绑定到Answer属性?

Do I need to work with the BindingSource? 我需要使用BindingSource吗?

BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = surveys;            
            dataGridView.DataSource = bindingSource;

Where surveys is the list.. 调查在哪里。

Thanks! 谢谢!

If you implement a ToString for Answer and Question classes, those will be displayed in the column. 如果为Answer和Question类实现ToString,则这些将显示在该列中。 Hope thats what you need. 希望这就是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM