1
votes

I have this 2 classes:

public class OrderDetail
{
    public int Id {get; set;}
    public int OrderId {get; set;}
    public virtual Product Product {get; set;}
}

public class Product
{
    public int Id {get; set;}
    public int Number {get; set;}
    public string Description {get; set;}
}

And I have a DataGridView bound to a binding source which in turn is bound to OrderDetail.

For the Product column, the grid currently displays the text MyAssembly.Product.

How can I set that column to display OrderDetail.Product.Description?

1

1 Answers

0
votes

Have you tried to override the ToString() method of the Product class?

The issue can be reproduced in a new fresh Console Application.

static void Main(string[] args)
{
    Product product = new Product
    {
        Id = 1,
        Number = 1,
        Description = "TheFirstProduct",
    };

    OrderDetail detail = new OrderDetail 
    {
        Id = 1,
        OrderId = 1,
        Product = product,
    };

    Console.WriteLine(detail.Product);
    Console.ReadLine();
}

The behavior of this code is the same as you report on DataGridView.

ConsoleApplication1.Product

But, if you overrides ToString() method on Product class like this:

public class Product
{
    public int Id { get; set; }
    public int Number { get; set; }
    public string Description { get; set; }

    public override string ToString()
    {
        return this.Description;
    }
}

... then the result of Console.WriteLine(detail.Product) may be the expected.

TheFirstProduct