Binding a DataGridViewColumn to a method as opposed to a property

I have a class that calculates the performance and changes in a list of values, a time-series. I calculate things like week's move, year-to-date, inception-to-date, and then the pattern kicks in. I need columns signifying performance every 3 Months, 6 Months, 12 Months, 24 Months, 36 Months, etc.

We wish to allow the user to dynamically add the columns as appropriate (as the series has 36, 48, 60 months worth of data). The classes i've created calculate these according to the number of weeks, and i can easily hard code the properties for the next 20 years, but that wouldn't be very good design.

C# doesn't support properties with parameters (i'm guessing VB.Net2005 still does). One solution (i think) would be to have a property such as Peformance(numberWeeks), and pass the parameter through with the DataPropertyName. We're writing in C# which wouldn't allow this, even if the grid would.

My next solution would be a method with the same signature, which is already there. It seems the datagridview doesn't bind to this, as the method is never called.

Another solution i suppose could be within reflection, and adding properties to the class at runtime. Not sure if this would work, and if so not sure if it would be a bit of an overkill.

Am i missing something obvious Can anyone help

Thanks,
Ben



Answer this question

Binding a DataGridViewColumn to a method as opposed to a property

  • Gboyega

    Actually databinding to a method isn't possible. You can create your own CustomPropertyDescriptor and override the GetValue method and get the value from your function or you can handle the DataGridView's CellFormatting event and get the value from your function, but the grid only binds to properties.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    thanks!

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"

     


  • Matthew Adams9346

    That helps a lot! Thank you for your quick and useful replies :-)

  • UntimelyWill

    Brilliant, thanks for the suggestions. I'll give it them a try, most likely the handling of the CellFormatting event.

    While we're here, can you confirm that the grid can bind to parameterised properties (ala VB) How does the binding work "under the hood"

  • Shyne79

    No, the grid doesn't work with parameterized properties. Parameterized properties in vb come across in IL as an named index property since the CLR doesn't actually support properties that have parameters.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    EDIT: So how the grid does its databinding is that it asks the CurrencyManager to return a collection of item properties via the CurrencyManager GetItemProperties() method:


    this.props = this.currencyManager.GetItemProperties();
     
    When we set a value, we call:

    this.props[boundColumnIndex].SetValue(this.currencyManager[rowIndex], value);
     
    And to get a value we call:

    value = this.props[boundColumnIndex].GetValue(this.currencyManager[rowIndex]);
     
    Hope this helps! 

     

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"

     


  • Binding a DataGridViewColumn to a method as opposed to a property