views:

43

answers:

2

Hey all,

I have following situation in a C# Windows Forms (.NET 4.0) application:

I have a bindingsource which datasource is set to a collection of objects of a custom class.

Most properties of this class can be represented by a textbox or checkbox, as they are strings or numbers or bools. However, there is one property, called Ratings, which is another custom object and must be represented by a custom control I developed.

I have bound a datagridview to the bindingsource, and obviously the dgview displays the numbers, strings and bools correctly. But on the same form I also have an instance of the custom control I developed, and so want I want to do now is bind my custom control to the same bindingsource as the datagridview uses, and to the specific datamember called Ratings. But I don't have the slightest clue on how to start. I suspect I must implement some interface which implements DataSource and DataMember on my control, but not sure where to begin. So basically what I want is each time the selected item is changed by means of selecting another row in the datagridview, the according Ratings property is represented in the custom control.

Right now the endresult that I wish to achieve is working, but I do this by refreshing my custom control each time the OnSelectedRowChanged event of the datagridview is firing, and associating the Ratings property of the selected item to the custom control.

But I was wondering if I could achieve this purely by the databinding mechanism in .NET.

Thanks for any replies !

Mathieu

ps: to further clarify, as it doesn't seem so clear in pure text:

Psuedo-code representing the object that I use as datasource for the bindingsource is:

public class Car
{

   private string number;
   private string name;
   ...
   private Ratings ratings;


   public string Number
   {
     get { return this.number; }
     set { this.number = value; }
   }

   public string Name
   {
     get { return this.name; }
     set { this.name = value; }
   }

   ...

   public Ratings Ratings
   {
     get { return this.ratings; }
     set { this.ratings = value; }
   }

}

So I want all the string and number properties from the bindingsource to bind to the datagridview, but I need the Ratings property to bind to the custom control.

+1  A: 

I understood that you'd like to have a special column in the DataGridView with your custom control. To do this, you need to create a subclass of DataGridViewCell and DataGridViewColumn and implement IDataGridViewEditingControl.

See MSDN for an example.

Timores
Nope, sorry if it wasn't clear. It's not a special column in the DataGridView that I want. I have a totally separate custom control which I use to represent the Ratings property. Nothing to do with the DataGridView. Basically I want all the properties of the object to bind to the DataGridView (which is the case now), with the exception of the Ratings property, which needs to bind to the custom control.
tjeuten
I posted some code in my question to make it more clear hopefully
tjeuten
So, you have a detail form where you'd like to show the ratings and you want the current item of the grid view to show its ratings in this custom control ?
Timores
This question http://stackoverflow.com/questions/245441/add-datasource-property-to-a-custom-winforms-control may be useful to you.
Timores
Thanks for the link. If I understand it correctly, as I need to bind my custom control to a single object (being the Ratings property of my Car class), I'll need to use the Simple Binding technique. This opposed to Complex Binding which is used to bind to a list of objects, right ?
tjeuten
Timores
A: 

You can read this Microsoft article about interfaces related to data binding. The ones of your interest are at the bottom under "Interfaces for Implementation by Component Authors" section.

Ivan Ferić