Home

ToString produces empty cells for interfaces in Data Grid

When dumping to a grid, when the type of a column is an interface and the implementing class overrides ToString, the cell is empty. It doesn't affect classes that don't override ToString()

This affects single and multi column grids

It doesn't occur if all the instance in the interface column are of the same type.

It also doesn't affect columns with abstract or concrete base class.

void Main()
{
    new ISomething[] {
         new ImplementedWithToString { Value = "Inner1" },
         new Implemented { Value = "Inner2" },
         new ImplementedWithToString { Value = "Inner3" },
    }.Dump();


    new[] {
            new WrappingClass
            {
                Interface = new ImplementedWithToString { Value =  "Inner1" },
                Concrete = new DerivedWithToString { Value = "Inner1" },
            },
            new WrappingClass
            {
                Interface = new Implemented { Value =  "Inner2" },
                Concrete = new Derived { Value = "Inner2" },
            },
            new WrappingClass
            {
                Interface = new ImplementedWithToString { Value =  "Inner3" },
                Concrete = new DerivedWithToString { Value = "Inner3" },
            },
        }.Dump();

    new ISomething[] {
            new ImplementedWithToString { Value = "Inner1" },
            new ImplementedWithToString { Value = "Inner2" },
            new ImplementedWithToString { Value = "Inner3" },
       }.Dump();

    new BaseClass[] {
        new DerivedWithToString { Value = "Inner1" },
        new Derived { Value = "Inner2" },
        new DerivedWithToString { Value = "Inner3" },
    }.Dump();
}

abstract class BaseClass { }

interface ISomething { }

class Implemented : ISomething
{
    public string Value { get; set; }
}

class ImplementedWithToString : ISomething
{
    public string Value { get; set; }
    public override string ToString() => $"Value: {Value}";
}

class Derived : BaseClass
{
    public string Value { get; set; }
}

class DerivedWithToString : BaseClass
{
    public string Value { get; set; }
    public override string ToString() => $"Value: {Value}";
}

class WrappingClass
{
    public ISomething Interface { get; set; }
    public BaseClass Concrete { get; set; }
}

Comments

Sign In or Register to comment.