Lets image that I have the following classes
public class Master
{
public string MasterName = "Something";
public List<Detail> details = new List<Detail>();
}
public class Detail
{
public string Foo = "Test";
}
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
Just do this:
Mask the property you want to get a childvalue from with [Browsable(false)] so it wont show up in the datagrid. Then create a NEW property in your class that holds the child object which has only a "get" method showing the childproperty value: For example:
[Browsable(false)] //Because we use the CreatorUsernameProperty to do this.
public virtual User Creator { get; set; }
[DisplayName("Creator")] //shows like this in the grid
public string CreatorUsername => Creator?.Username;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With