WinForms: Binding DataGridView to a List of Custom Objects with Complex Properties
Can someone help me understand I've searched everywhere and can't find a clear answer. I'm trying to bind a `DataGridView` to a list of custom objects that contain complex properties, but I'm running into issues where the grid does not display the expected values. My custom object looks like this: ```csharp public class Product { public string Name { get; set; } public Category ProductCategory { get; set; } } public class Category { public string CategoryName { get; set; } public string Description { get; set; } } ``` I am attempting to bind a `List<Product>` to the `DataGridView` as follows: ```csharp List<Product> products = new List<Product> { new Product { Name = "Laptop", ProductCategory = new Category { CategoryName = "Electronics", Description = "Devices" } }, new Product { Name = "Chair", ProductCategory = new Category { CategoryName = "Furniture", Description = "Home and Office" } } }; dataGridView1.DataSource = products; ``` However, the `DataGridView` only shows the `Name` property and does not display anything for `ProductCategory`. I’ve tried setting up the `DataGridView` columns manually like this: ```csharp dataGridView1.Columns.Clear(); dataGridView1.Columns.Add("ProductName", "Product Name"); dataGridView1.Columns.Add("ProductCategory", "Category"); dataGridView1.Rows.Add("Laptop", "Electronics"); dataGridView1.Rows.Add("Chair", "Furniture"); ``` But that feels clunky and doesn't use the benefits of data binding. When I run this, I encounter an behavior about the value not being found in the `DataSource`. I also tried using a custom property in the `Product` class to expose the `CategoryName` directly: ```csharp public string CategoryName => ProductCategory.CategoryName; ``` Even after adding this property and updating my binding to use it, the `DataGridView` still fails to show the category name correctly. I'm using .NET Framework 4.8 and Visual Studio 2019. What am I missing here? Is there a better way to handle this type of binding? Any help would be greatly appreciated! This is part of a larger application I'm building. Is there a better approach? I'm working on a service that needs to handle this. The stack includes C# and several other technologies.