Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Binding a DataGridViewColumn to a method as opposed to a property
 

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

Ben Hart  Thursday, January 12, 2006 9:58 AM

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.

 

thanks!

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

 

Mark Rideout  Thursday, January 12, 2006 5:52 PM

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.

 

thanks!

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

 

Mark Rideout  Thursday, January 12, 2006 5:52 PM
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"?
Ben Hart  Friday, January 13, 2006 7:02 AM

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.

 

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"

 

Mark Rideout  Friday, January 13, 2006 5:43 PM
That helps a lot! Thank you for your quick and useful replies :-)
Ben Hart  Saturday, January 14, 2006 10:19 AM

You can use google to search for other answers

Custom Search

More Threads

• Datagridview - Error when trying to make a multiline cell
• without using "NoKeyUpCombo.vb" and "DataGridComboBoxColumn.vb" can i add combobox in datagrid?
• Datagridview sort not working as expected with combo box cells
• How to specify a cell in a dataGridView
• DataGridView Scrolling Issue
• DataGrid Navigation
• Filtering data in a datagridview from a record in a form
• show the data from a varibel in a report
• Drive list box + Dir list box
• Update changes in a cell of a DataGridView to datatable without move to next cell