Uros Calakovic
Uros Calakovic

Reputation:

DataGridView bound to custom class

I used this thread's answer to bind a DataGridView to a dictionary:

DataGridView bound to a dictionary

This works fine if I use Dictionary<string, double>, but if I use a class instead of string (Dictionary<MyClass, double>) the DataGridView displays the string representation of MyClass. MyClass has a string property (Value) that I would like to be displayed in the DataGridViwe column. Is there a way to accomplish this?

Upvotes: 1

Views: 888

Answers (1)

Chris Ballard
Chris Ballard

Reputation: 3769

The simplest method would be to implement ToString() in your class, if this is an option, eg:

public class MyClass
{
    public string Value { get; set; }

    public override string ToString()
    {
        return Value;
    }

    // ...
}

Upvotes: 1

Related Questions