Monday, April 11, 2011

What's the easiest way to rename the columns of a DataGridView showing a List<T>?

I have a DataGridView, whose DataSource I am setting to a List<T>. T in this case is a class with a property called Foo, whose header I want to show as Foo bar.

If it was a datatable, I could just change the query:

select Foo as [Foo bar] from Baz

But with something like this, where I'm setting the DataGridView's DataSource to a List<Baz>:

public class Baz {
   public string Foo { get; set; }
}

I can't rename "Foo" to "Foo bar" because it contains spaces. Do I have to rename the DataGridViewColumn manually?

The most awesome thing would be if I could use class decorators, something like this:

public class Baz {
   [DataGridViewColumnTitle("Foo bar")]
   public string Foo { get; set; }
}

But I as far as I can see, nothing like that exists in the standard library.

What's my best option?

From stackoverflow
  • [DisplayName("Foo bar")]
    

    (in the System.ComponentModel namespace; MSDN)

    Blorgbeard : Beautiful! I knew it had to exist :)

0 comments:

Post a Comment