DataGridView Annoyance
I have grown to love the DataGridView and the simplicity it provides in displaying lists of objects. All you have to do is set the DataGridView’s DataSource property to the list that you want displayed and it does everything else.
Here’s an example
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<Person> people = new List<Person>();
people.Add(new Person() { FirstName = "John", LastName = "Smith" });
people.Add(new Person() { FirstName = "John", LastName = "Doe" });
people.Add(new Person() { FirstName = "Jane", LastName = "Does" });
dataGridView1.DataSource = people;
}
}
And here is the screenshot:
Yesterday I had a list that contained simple string objects (List<string>) and I wanted to display the list in a DataGridView, but I was thoroughly frustrated when the DataGridView did not show the string items, but instead displayed the Length of each string item. Below is an example.
public Form1()
{
InitializeComponent();
List<string> fullNames = new List<string>();
fullNames.Add("John Smith");
fullNames.Add("John Doe");
fullNames.Add("Jane Doe");
dataGridView1.DataSource = fullNames;
}
And here is the screenshot:
Now why in the world would I want to know the length of a string object? I could care less. I just want the DataGridView to display the string itself, not how long it is.
I thought there was an easy way around this, so I started googling… and googling, and to my dismay I did not find a work around.
I did find the reason that the DataGridView displays the length. When a DataGridView is bound to a list, it uses reflection to display all the public properties of the object type in the list. In the first example, the Person type contained 2 properties: FirstName and LastName. And those were displayed by the DataGridView. The second example contained a list of string items, and it turns out that the only public property for strings is the Length property. It does not have a Value property. It does have a ToString() method, but that is a method, not a property.
There is a workaround, but it’s annoying. Basically, you need to create a type that has a public property that returns the string value. Below is an example
public class StringValue
{
public StringValue(string Value)
{
this.Value = Value;
}
public string Value { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<StringValue> fullNames = new List<StringValue>();
fullNames.Add(new StringValue("John Smith"));
fullNames.Add(new StringValue("John Doe"));
fullNames.Add(new StringValue("Jane Doe"));
dataGridView1.DataSource = fullNames;
}
}
And now the DataGridView correctly shows the string items
I wish the DataGridView provided the option to manually set which properties, and even method return values, to display. I don’t think that will happen any time soon, so I guess I will just have to deal with the hack.
jon
September 9th, 2008 at 10:18 am #
Congrats on your first post! I’m subscribed! Looking forward to the next one.