Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Attribute I can use on a property to tell DataGridView how to format the column?

Following on a bit from this question, if I have this class:

private class MyClass {
  [DisplayName("Foo/Bar")]
  public string FooBar { get; private set; }
  public decimal Baz { get; private set; }
}

And I want to display a List<MyClass> in a DataGridView (with autogenerated columns), what's the easiest way to make the Baz column display formatted as currency?

Is there an attribute I can use like I'm using DisplayName, or do I have to mess with the columns after they are created?

like image 954
Blorgbeard Avatar asked Jul 06 '09 03:07

Blorgbeard


Video Answer


1 Answers

I know its not perfect but you could add another property called CurrencyBaz that would basically return a formatted Baz, then bind that to the grid instead of the real Baz.

so something like this.

private class MyClass {
  [DisplayName("Foo/Bar")]
  public string FooBar { get; private set; }
  [Browsable(False)]
  public decimal Baz { get; private set; }
  [DisplayName("Baz")]
  public CurrencyBaz
  {
        get { return string.Format(Baz, "C2"); }
  }
}
like image 102
kay.one Avatar answered Sep 23 '22 16:09

kay.one